Answer the question
In order to leave comments, you need to log in
Do I need to allocate a separate thread for transactions in Hibernate?
1. Do I need to allocate a separate thread for transactions in Hibernate?
2. If an object was loaded via session.load, and then this object is no longer used, will Hibernate keep its cache? Do I need to remove unnecessary objects from the cache?
3. Is it possible to use Hibernate CRUD functions through one transaction for the entire application? Or in what way it is possible to minimize opening/closing of transactions?
Answer the question
In order to leave comments, you need to log in
If you use EJB, then all requests from the ORM to the database turn into a transaction, if you add the attribute transaction-type="JTA
. Otherwise, you will steer transactions manually . It is enough just to receive a transaction, well, start / commit / make a rollback. The transaction manager is already configured. If you do not use EJB, but manage with a spring, then it has its own transaction manager, xs, do I need to configure it separately.
It is these same transaction managers and resource managers that drive threads, connection pools, etc. It itself tracks any attempts to access the database and wraps it in a transaction (if there is a running transaction, it doesn’t matter if you started it yourself, or the application server did it for you). These managers support both local and distributed transactions, and are all thread-safe.
If you do not use JPA and EntityManager, only Hibernate, then you can take an implementation of a transaction manager, such as Atomikos , configure and create it manually, and then manually start transactions as well.
Using 1 transaction for the entire application is stupid. Multithreading will disappear (after all, 1 transaction cannot be tied to several threads), and it’s bad to do many actions within 1 transaction.
In general, you need to use everything wisely. If transactions are necessary - use. Only correctly: in 1 transaction there should be 1 logical operation.
PS Oh yes, if I'm not mistaken, then the number of threads depends more on the resource manager, and not on the transaction manager. Resources can be, for example, a database, or JMS messages. As you set up the DataSource , so be it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question