Announcement Announcement Module
No announcement yet.
Spring + Plain JDBC + Transaction Management Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + Plain JDBC + Transaction Management

    I am trying to use transaction management with Spring and Plain JDBC(not JDBC template provided in the Spring jar).

    But due to some reason the Roll back is not happening.

    Here' my application Context

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    xmlns:xsi="" xmlns:context=""
    xmlns:tx="" xmlns:aop=""

    <bean id="myImpl" class="">
    <property name="myServices" ref="myServices" />

    <bean id="myServices" class="">
    <property name="myDAO" ref="myDAO" />

    <!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/>
    bean below) -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!-- the transactional semantics... -->
    <!-- all methods starting with 'get' are read-only -->
    <tx:method name="get*" read-only="true" />
    <!-- other methods use the default transaction settings (see below) -->
    <tx:method name="*" />

    <!-- ensure that the above transactional advice runs for any execution of
    an operation defined by the myService interface -->
    <aopointcut id="myServiceOperation"
    expression="execution(**.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="myServiceOperation" />

    <bean id="myDAO" class="">
    <property name="dataSource" ref="dataSource" />
    <!-- <tx:jta-transaction-manager /> -->

    <!-- <jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/MySqlDS"
    /> -->
    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://" />
    <property name="username" value="root" />
    <property name="password" value="****" />

    <!-- <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager">
    <property name="transactionManagerName" value="java:/TransactionManager"/>
    <property name="userTransactionName" value="java:comp/UserTransaction"> </property>
    </bean> -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">


  • #2
    How do you get a connetion when using plain jdbc? You shouldn't call dataSource.getConnection(), the right way is to call DataSourceUtils.getConnection(dataSource) to obtain a connection bound to a thread and participating in a transaction.
    If this is not possible (legacy code), look at TransactionAwareDataSourceProxy (DataSource wrapper).


    • #3
      I changed that to get the TransactionAwareDataSourceProxy .But it does not seem to work .

      <bean id="dataSource"
      class="org.springframework.jdbc.datasource.Transac tionAwareDataSourceProxy" >
      <property name="targetDataSource" ref="testDataSource"/>

      <bean id="billerDAO" class="">
      <property name="ds" ref="dataSource" />


      • #4
        Transaction manager has to work with original dataSource, not the proxy.

        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
        as you defined it should have parameter named dataSource, put there the original (not proxied) testDataSource.

        If it still does not work, do some logging on DataSourceTransactionManager to see that transactions are used (tx advice is applied to a method you want). You can also do some debugging on the proxy (especially DataSourceUtils.doGetConnection(..) method it delegates to) to see where it takes a connection from.

        You said that no rollback is performed. If you run Spring integration tests, check that you set up @TransactionConfiguration well - testDataSource, defaultRollback to true.