Announcement Announcement Module
Collapse
No announcement yet.
Spring + Plain JDBC + Transaction Management Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.1.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">


    <bean id="myImpl" class="com.my.api.myImpl">
    <property name="myServices" ref="myServices" />
    </bean>

    <bean id="myServices" class="com.my.services.myServicesImpl">
    <property name="myDAO" ref="myDAO" />
    </bean>

    <!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/>
    bean below) -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
    <!-- 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="*" />
    </tx:attributes>
    </tx:advice>

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



    <bean id="myDAO" class="com.my.dao.myDAOImpl">
    <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- <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://172.16.175.12:3306/my" />
    <property name="username" value="root" />
    <property name="password" value="****" />
    </bean>


    <!-- <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">

    </bean>
    </beans>

  • #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).

    Comment


    • #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>


      <bean id="billerDAO" class="com.my.dao.BillerDAOImpl">
      <property name="ds" ref="dataSource" />
      </bean>

      Comment


      • #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.

        Comment

        Working...
        X