How to set a request for reading without restrictions?

with hibernation, how can I establish that the request will be considered uncommitted? I do not want this to be a global parameter, just need to do this based on the request.

+4
source share
5 answers

Have you tried session.connection().setTransactionIsolation(...) ?

PS For modern DBMS-based MVCCs, you do not need to use โ€œread uncomittedโ€ to avoid blocking. Some DBMSs do not even have this level of isolation.

+2
source

The Hibernate API alone does not allow you to do this programmatically, as I know. The docs say that you can specify the isolation level as a configuration property:

hibernate.connection.isolation - Sets the isolation level of a JDBC transaction. Check java.sql.Connection connection for meaningful values

But of course this applies to the entire SessionFactory , and not just to specific requests.

It is tempting to bother with the basic isolation level of java.sql.Connection , but I would be very careful about this, you run the risk of hibernate locking strategies that use the JDBC isolation level to determine which LockMode will be used. If you change this directly to Connection, you may get odd results. Of course, you may not have a choice, but be careful.

The โ€œbestโ€ solution would be to use the Hibernate Spring Transaction API , which allows you to separate transaction semantics, such as the isolation level from the Hibernate API, in a completely, predictable and reliable way.

+2
source

Set transaction isolation level:

 session.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 
+1
source

Using Spring with Hibernate, you can have Spring transaction management with annotations, i.e. a configuration like the one below in Spring applicationContext.xml:

 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- To use annotation driven in aspect mode (mode="aspectj"), it is necessary to add spring-aspects lib --> <tx:annotation-driven transaction-manager="transactionManager" /> 

The best way to achieve this is to use an annotation like the one below:

 @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_UNCOMMITTED) 
+1
source

by default, statements are not executed in sleep mode until you do this explicitly (or the connection returns to the pool and the driver makes a commit for you, which is not guaranteed). so it looks like he is doing what you want.

you can also simply cancel the current transaction if you want to cancel the statements made since the start of the transaction.

in mysql, you can do something like:

 set session transaction isolation level read uncommitted 

or use the axtavt method,

call getTransactionIsolation () before the request to get the current mode so you can return it after stmt if you want.

0
source

All Articles