Spring aop transaction not committing the transaction with mybatis

364 views
Skip to first unread message

Kamran Yadav

unread,
Oct 27, 2010, 11:09:19 PM10/27/10
to mybati...@googlegroups.com

I am using Spring 3.0.3, MyBatis 3.0.2 and mybatis-spring 1.0.0 on Apache Tomcat 6.0.29.

I have used declarative transactions of spring for transaction management. The issue is that the transactions are not working for me. The transaction works for sometime and then suddenly stops working. I can go and do multiple inserts and updates and all will work for 1 time or 10 time or 20 time there no fix patterna dn suddenly stops.

I even tried the annotation driven transaction but the issue is same. I tried the session open and close on the template and that works but that does not help as I don't to maintain transaction in my DAO layer but manage via service layer.

I have tried checking all my configs but no clue. Any pointers?

My configurations looks like this -

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
       
<tx:attributes>
           
<tx:method name="*" propagation="REQUIRED" />
       
</tx:attributes>
   
</tx:advice>

   
<aop:config>
       
<aop:pointcut id="dtxops"
           
expression="execution(* com.service.*.*(..))" />
       
<aop:advisor advice-ref="txAdvice" pointcut-ref="dtxops" />
   
</aop:config>

TX Manager and DS config -

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

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
       
<property name="resourceRef" value="true" />
       
<property name="jndiName" value="java:comp/env/jdbc/MyDBInstance" />
   
</bean>

Eduardo

unread,
Oct 28, 2010, 1:04:32 AM10/28/10
to mybatis-user
That config seems right. Could you post the rest of your appContext
(services + mappers)?

Kamran Yadav

unread,
Oct 28, 2010, 2:34:09 AM10/28/10
to mybati...@googlegroups.com
Here are the details, let me know if any more info is required.

Person.xml mapper file

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.api.Person">
<resultMap type="Person" id="personResultMap">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="description" column="description" />
</resultMap>

<insert id="create" parameterType="Person">
insert into
person
(id,name,description)
values
(#{id},#{name},#{description})
</insert>
</mapper>

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.api.Person"
alias="Person" />
</typeAliases>

<mappers>
<mapper resource="dao/ibatis/Person.xml" />
</mappers>
</configuration>

Related spring config and also default-autowire="byName" is set.

<context:component-scan base-package="com.accept.ppm.dao">
<context:include-filter type="annotation"
            expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
<context:component-scan base-package="com.accept.ppm.service">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Component" />
</context:component-scan>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation">
<value>classpath:/dao/ibatis/SqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>

DAO class -

public interface PersonDAO {
void create(Person person);
}

@Repository("personDAO")
public class SqlMapPersonDAO extends SqlSessionDaoSupport implements PersonDAO {
        @Override
public void create(Person person) {
SqlSessionTemplate template = getSqlSessionTemplate();
template.insert("com.api.Person.create", person);
}
}

Service class -

public interface PersonService {
void create(Person person);
}

@Component("personService")
public class PersonServiceImpl extends PersonService {
        private PersonDAO personDAO;

public void setPersonDAO(PersonDAO personDAO) {
this.personDAO= personDAO;
}

        @Override
public void create(Person person) {
SqlSessionTemplate template = getSqlSessionTemplate();
template.insert("com.api.Person.create", person);
}
}

Eduardo

unread,
Oct 28, 2010, 2:41:19 AM10/28/10
to mybatis-user
I cannot see anything wrong.

Maybe you could execute your app with a logger "org.mybatis.spring"
set to debug to see what is happening?

Kamran Yadav

unread,
Oct 28, 2010, 3:08:26 AM10/28/10
to mybati...@googlegroups.com
I have already done that but nothing in the logs.

One more observation - once commit stops working it never recovers for that http session until a new http session is created. Also if one user session gets the problem all the http session starts getting the same issue. Now if i create a new http session then all the previous http session starts working.

Eduardo

unread,
Oct 28, 2010, 8:03:40 AM10/28/10
to mybatis-user
But something should be printed. I.e. this is the output of the test:

Searching for MyBatis mappers in 'org.mybatis.spring.sample.mapper'
package
Registering MyBatis mappers
Registering MyBatis mapper with 'interface
org.mybatis.spring.sample.mapper.UserMapper' mapperInterface
Property 'configLocation' not specified, using default MyBatis
Configuration
Property 'mapperLocations' was not specified, only MyBatis mapper
files specified in the config xml were loaded
Property 'configLocation' not specified, using default MyBatis
Configuration
Property 'mapperLocations' was not specified, only MyBatis mapper
files specified in the config xml were loaded
Creating SqlSession from SqlSessionFactory
Registering transaction synchronization for SqlSession
Transaction synchronization committed SqlSession
Transaction synchronization closed SqlSession

Regarding the session..what data are you storing in your session?


On 28 oct, 09:08, Kamran Yadav <kamranya...@gmail.com> wrote:
> I have already done that but nothing in the logs.
>
> One more observation - once commit stops working it never recovers for that
> http session until a new http session is created. Also if one user session
> gets the problem all the http session starts getting the same issue. Now if
> i create a new http session then all the previous http session starts
> working.
>

Kamran Yadav

unread,
Oct 28, 2010, 8:12:10 AM10/28/10
to mybati...@googlegroups.com
I am not storing anything in the session.

I am getting only this in the log at DEBUG level -

DEBUG 2010-10-28 17:40:57,312 [http-9090-1] org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
DEBUG 2010-10-28 17:40:57,752 [http-9090-1] org.mybatis.spring.SqlSessionUtils - Creating SqlSession from SqlSessionFactory

Eduardo

unread,
Oct 28, 2010, 9:20:19 AM10/28/10
to mybatis-user
Could you mail us some code to see if we can reproduce it?

Hunter

unread,
Oct 28, 2010, 10:25:13 AM10/28/10
to mybatis-user
If you never see something like the following in the debug output,
something is wrong:
DEBUG springframework.jdbc.datasource.DataSourceUtils Returning JDBC
Connection to DataSource

What is the code that is calling PersonService.create()? I assume it's
just a web request?

Kamran Yadav

unread,
Oct 29, 2010, 8:28:32 AM10/29/10
to mybati...@googlegroups.com
I see this in the logs and there is no "Returning JDBC Connection to DataSource" kind of message.

[10-29 16:50:00] DEBUG org.springframework.jdbc.datasource.DataSourceUtils [http-8080-1]: Fetching JDBC Connection from DataSource
[10-29 16:50:00] DEBUG org.mybatis.spring.SqlSessionUtils [http-8080-1]: Creating SqlSession from SqlSessionFactory
[10-29 16:50:00] DEBUG java.sql.Connection [http-8080-1]: ooo Connection Opened
[10-29 16:50:00] DEBUG java.sql.PreparedStatement [http-8080-1]: ==>  Executing: INSERT INTO person(id, name, description) VALUES(?, ?, ?) 
[10-29 16:50:00] DEBUG java.sql.PreparedStatement [http-8080-1]: ==> Parameters: 7(Integer), asdasds(String), asdasdas(String)
[10-29 16:50:00] DEBUG java.sql.Connection [http-8080-1]: xxx Connection Closed


I have
mybatis-test.zip

Eduardo

unread,
Oct 29, 2010, 12:29:02 PM10/29/10
to mybatis-user
Thanks for the code Kamran.

I have not run it yet but this config:
<aop:config>
<aop:pointcut id="dtxops"
expression="execution(* com.accept.ppm.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="dtxops" />
</aop:config>

is not in agreement with your service package
com.mybatis.test.service. Maybe it is just that you changed the code
to mail it and forgot to change that but anyway I would try it with

<aop:config>
<aop:pointcut id="dtxops"
expression="execution(* com.mybatis.test.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="dtxops" />
</aop:config>

Please try it and tell us how it went.
> ...
>
> leer más »
>
>  mybatis-test.zip
> 15 KVerDescargar

Eduardo

unread,
Oct 29, 2010, 3:43:55 PM10/29/10
to mybatis-user
I have runned it in glassfish and mysql and it worked fine... seems
that there is no problem with transactions.

I ran it with mybatis-spring.1.0.0-RC1 and dwr-3.0.M1.jar but I
suppose your versions wil work fine as well.
> ...
>
> leer más »

Hunter

unread,
Oct 29, 2010, 4:27:32 PM10/29/10
to mybatis-user
I ran it in Jetty, modified to use hsqldb and didn't see any problems
either other than the one Eduardo pointed out.

You can use the following config in log4j:
log4j.logger.org.apache.ibatis=INFO
log4j.logger.org.mybatis.spring=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.org.springframework.jdbc=DEBUG

You should see something like:
[10-29 16:25:57] DEBUG
org.springframework.jdbc.datasource.DataSourceTransactionManager
[main]: Creating new transaction with name
[com.mybatis.test.service.impl.PersonServiceImpl.create]:
PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[10-29 16:25:57] DEBUG
org.springframework.jdbc.datasource.SimpleDriverDataSource [main]:
Creating new JDBC Driver Connection to [jdbc:hsqldb:mem:dataSource]
[10-29 16:25:57] DEBUG
org.springframework.jdbc.datasource.DataSourceTransactionManager
[main]: Acquired Connection [org.hsqldb.jdbc.jdbcConnection@8dc1f04]
for JDBC transaction
[10-29 16:25:57] DEBUG
org.springframework.jdbc.datasource.DataSourceTransactionManager
[main]: Switching JDBC Connection
[org.hsqldb.jdbc.jdbcConnection@8dc1f04] to manual commit
[10-29 16:25:57] DEBUG org.mybatis.spring.SqlSessionUtils [main]:
Creating SqlSession from SqlSessionFactory
[10-29 16:25:57] DEBUG java.sql.Connection [main]: ooo Connection
Opened
[10-29 16:25:57] DEBUG org.mybatis.spring.SqlSessionUtils [main]:
Registering transaction synchronization for SqlSession
[10-29 16:25:57] DEBUG java.sql.PreparedStatement [main]: ==>
Executing: INSERT INTO person(id, name, description) VALUES(?, ?, ?)
[10-29 16:25:57] DEBUG java.sql.PreparedStatement [main]: ==>
Parameters: 820511(Integer), 375a4629(String), test 820511(String)
[10-29 16:25:57] DEBUG
org.springframework.jdbc.datasource.DataSourceTransactionManager
[main]: Initiating transaction commit
[10-29 16:25:57] DEBUG
org.springframework.jdbc.datasource.DataSourceTransactionManager
[main]: Committing JDBC transaction on Connection
[org.hsqldb.jdbc.jdbcConnection@8dc1f04]
[10-29 16:25:57] DEBUG org.mybatis.spring.SqlSessionUtils [main]:
Transaction synchronization committed SqlSession
[10-29 16:25:57] DEBUG org.mybatis.spring.SqlSessionUtils [main]:
Transaction synchronization closed SqlSession
[10-29 16:25:57] DEBUG
org.springframework.jdbc.datasource.DataSourceTransactionManager
[main]: Releasing JDBC Connection
[org.hsqldb.jdbc.jdbcConnection@8dc1f04] after transaction
[10-29 16:25:57] DEBUG
org.springframework.jdbc.datasource.DataSourceUtils [main]: Returning
JDBC Connection to DataSource
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages