Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting isolation level in spring annotation-based transactions

I use in my project annotation-based transaction management (I annotate some methods with @Transactional). I would like to set the isolation level globally (not by putting it as an argument to each @Transactional annotation).

Is it possible configure that in the XML? Currently my xml configuration contains

<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource" />
</bean>

Is it possible to add the isolation somehow to tx:annotation-driven?

like image 935
jfu Avatar asked Mar 03 '11 13:03

jfu


1 Answers

Spring's transaction management sets the transaction isolation on the Connection if you configure a non-default transaction isolation (by specifying it in a @Transactional annotation for example). If you can configure the transaction isolation of the connections while ensuring no other mechanism changes the transaction isolation of the connections, then you in effect globally set the transaction isolation used by the application.

For example, the Commons DBCP BasicDataSource class defines the defaultTransactionIsolation property to set the transaction isolation of connections returned from the pool:

<bean
    id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
  <property name="defaultTransactionIsolation">
    <util:constant static-field="java.sql.Connection.TRANSACTION_READ_COMMITTED"/>
  </property>
</bean>
like image 196
Chin Huang Avatar answered Oct 22 '22 12:10

Chin Huang