Spring Hibernate, Avoid Repeating Statements and Closing

I have been optimizing the algorithm, and I noticed that Hibernate creates and releases update statements again, rather than reusing them. All of them from one request.

15:57:31,589 TRACE [.JdbcCoordinatorImpl]:371 - Registering statement [sql : 'update ... 15:57:31,591 TRACE [.JdbcCoordinatorImpl]:412 - Releasing statement [sql : 'update ... 15:57:31,592 TRACE [.JdbcCoordinatorImpl]:525 - Closing prepared statement [sql : 'update ... 15:57:31,592 TRACE [.JdbcCoordinatorImpl]:278 - Starting after statement execution processing [ON_CLOSE] 15:57:31,594 TRACE [.JdbcCoordinatorImpl]:371 - Registering statement [sql : 'update ... 15:57:31,595 TRACE [.JdbcCoordinatorImpl]:412 - Releasing statement [sql : 'update ... 15:57:31,596 TRACE [.JdbcCoordinatorImpl]:525 - Closing prepared statement [sql : 'update ... 15:57:31,596 TRACE [.JdbcCoordinatorImpl]:278 - Starting after statement execution processing [ON_CLOSE] 15:57:31,597 TRACE [.JdbcCoordinatorImpl]:371 - Registering statement [sql : 'update ... 15:57:31,599 TRACE [.JdbcCoordinatorImpl]:412 - Releasing statement [sql : 'update ... 15:57:31,600 TRACE [.JdbcCoordinatorImpl]:525 - Closing prepared statement [sql : 'update ... 15:57:31,601 TRACE [.JdbcCoordinatorImpl]:278 - Starting after statement execution processing [ON_CLOSE] 

The main method of the algorithm has the annotation @Scope and a @Transactional . The expected behavior is that if something goes wrong, the ROLLBACK algorithm updates.

The algorithm below uses @Service , which has a different @Scope and also @Transactional . A service is one that uses Hibernate to update the database using session.update(entity) . The documentation says that by default, nested transactions reuse the transaction if it exists.

  • Is this statement correct?
  • Can changing a region create problems?
  • How can I use Hibernate to reuse a statement during a transaction?

Thank you for attention

+5
source share
1 answer

Your understanding is correct. The scope is not related to how transactions are propagated; Spring should wrap your beans with proxies that manage transactions regardless of scope.

Cannot reuse instructions when using Hibernate. Even when writing JDBC code manually, this is not recommended, due to the complexity of the code that this approach forces. A common response to this is to use a ready-made instruction cache in the JDBC connection pool. For example, using the Apache DBCP pool, you can use poolPreparedStatements and maxOpenPreparedStatements to manage this. Pools bundled with application servers have similar settings.

+5
source

Source: https://habr.com/ru/post/1236444/


All Articles