user1652210
user1652210

Reputation: 56

Spring Mybatis Tomcat - Transactions not using Spring

Spring Transaction is not working for below configuration using TOMCAT.

spring: version: 3.1.2.RELEASE
spring.ws:version:2.0.5.RELEASE
Mybatis: version:3.1.1
Database: Oracle 11g
Datasource connection:com.oracle.ojdbc6.11.2.0
Server : Tomcat : apache-tomcat-6.0.32

We are trying to create transactional service using AOP declarative transaction implementation. The setup is working fine with transactions being created as expected during Integration test, but while running the test from SoapUi using the endpoints, on local Server(Tomcat), All the transactions are non-transactional (can see the same in log files)

SPRING-DAOS.xml

 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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="mywebappActiveTransactionManager" />

<aop:aspectj-autoproxy/>

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

<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
<tx:advice id="txAdvice" transaction-manager="mywebappActiveTransactionManager">
    <tx:attributes>
        <tx:method name="*" rollback-for="SQLException"/>
        <!-- <tx:method name="*" read-only="false" propagation="REQUIRED" /> -->
        <!--  <tx:method name="*" read-only="false" propagation="REQUIRED" />  -->
    </tx:attributes>
</tx:advice>


<jee:jndi-lookup id="mywebappActiveDataSource" jndi-name="java:/comp/env/jdbc/mywebapp-active" /> 

<bean id="mywebappActiveTransactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="mywebappActiveDataSource" />
</bean>

<bean id="mywebappActiveSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="mywebappActiveDataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>

<bean id="mywebappActiveMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.org1.org2.mywebappgate.dao" />
    <property name="sqlSessionFactoryBeanName" value="mywebappActiveSessionFactory" />
</bean>

Tomcat Context.xml

<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>


<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/mywebapp-active" password="xxxxxx" type="javax.sql.DataSource" url="jdbc:oracle:thin:@mywebappdc1.company:1521:mywebappdc1" username="xxxxxx"/>

mybatis-config.xml

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
    <!-- changes from the defaults -->
    <setting name="lazyLoadingEnabled" value="false" />
</settings>

<mappers>
    <mapper resource="company/mywebappgate/dao/SimpleCommentDao.xml" /> 
    <mapper resource="company/mywebappgate/dao/CommentDao.xml"/>
    <mapper resource="company/mywebappgate/dao/ARDAO.xml"/>
    <mapper resource="company/mywebappgate/dao/ASIndicatorsDao.xml"/>
</mappers>

Junit Test Log :

org.mybatis.spring.SqlSessionUtils: Creating a new SqlSession
org.mybatis.spring.SqlSessionUtils: Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44]
org.mybatis.spring.transaction.SpringManagedTransaction: JDBC Connection [oracle.jdbc.driver.T4CConnection@1d5d765] will be managed by Spring
org.mybatis.spring.SqlSessionUtils: Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44]
org.mybatis.spring.SqlSessionUtils: Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44] from current transaction
org.mybatis.spring.SqlSessionUtils: Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44]
org.mybatis.spring.SqlSessionUtils: Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44]
org.springframework.context.support.GenericApplicationContext: Closing org.springframework.context.support.GenericApplicationContext@1b4fad5: startup date [Wed Sep 05 14:16:30 EDT 2012]; root of context hierarchy
org.springframework.beans.factory.support.DefaultListableBeanFactory: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@69d02b: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionalOperation,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,txAdvice

Tomcat Log :

org.mybatis.spring.SqlSessionUtils: Creating a new SqlSession
org.mybatis.spring.SqlSessionUtils: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1e55d39] was not registered for synchronization because synchronization is not active
org.mybatis.spring.transaction.SpringManagedTransaction: JDBC Connection [jdbc:oracle:thin:@mywebapp1.company:1521:mywebapp1, UserName=user, Oracle JDBC driver] will not be managed by Spring
org.mybatis.spring.SqlSessionUtils: Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1e55d39]
org.mybatis.spring.SqlSessionUtils: Creating a new SqlSession
org.mybatis.spring.SqlSessionUtils: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5b02a6] was not registered for synchronization because synchronization is not active
org.mybatis.spring.transaction.SpringManagedTransaction: JDBC Connection [jdbc:oracle:thin:@mywebappdc1.company:1521:mywebappdc1, UserName=mywebappSOA01, Oracle JDBC driver] will not be managed by Spring

Upvotes: 4

Views: 13216

Answers (1)

Ian Lim
Ian Lim

Reputation: 4284

Should not need to configure the xml in mybatis-*.xml if you are using Spring. You just need to include one property in mywebappActiveSessionFactory:

<property name="mapperLocations" 
          value="classpath*:company/mywebappgate/dao//*Mapper.xml"/>

Your mybatis-config.xml will then be

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
    <!-- changes from the defaults -->
    <setting name="lazyLoadingEnabled" value="false" />
</settings>

Upvotes: 0

Related Questions