Results 1 to 4 of 4

Thread: why spring3+ibatis3 Transaction manager has no effect?

  1. #1
    Join Date
    Jul 2010
    Posts
    2

    Default why spring3+ibatis3 Transaction manager has no effect?

    i use spring manage the Transaction and add the Transaction manager into the service class ,but the transaction do not rollback

    the config:


    Code:
    <?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:p="http://www.springframework.org/schema/p"
    	xmlns:jee="http://www.springframework.org/schema/jee" 
    	xmlns:tx="http://www.springframework.org/schema/tx" 
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context" 
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    		p:driverClassName="com.mysql.jdbc.Driver" 
    		p:url="jdbc:mysql://localhost:3306/test" 
    		p:username="root"
    		p:password="test"/>
    
    	<bean id="userService" class="com.test.service.UserService"> 
    		<property name="userMapper" ref="userMapper" />
    	</bean>
    	
    
    
    	<tx:advice id="txAdvice" transaction-manager="txManager">
    		<tx:attributes>
    			   <tx:method name="save*" propagation="REQUIRED"/>
    			   <tx:method name="delete*" propagation="REQUIRED"/>
    			   <tx:method name="update*" propagation="REQUIRED"/>
    			   <tx:method name="*" read-only="true" />
    		</tx:attributes>
    	</tx:advice>
    
    	<aop:config>
    		<aop:pointcut id="ServiceOperation" expression="execution(* com.test.interfaces.IService.*(..))" />
    		<aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceOperation" />
    	</aop:config>
    
    	<bean id="txManager"
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    
    	<bean id="sqlSessionFactory" class="org.springframework.orm.ibatis3.SqlSessionFactoryBean">
    		<property name="configLocation" value="classpath:config/config.xml" />
    		<property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    
    	<bean id="userMapper"
    		class="org.springframework.orm.ibatis3.support.MapperFactoryBean">
    		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
    		<property name="mapperInterface" value="com.test.dao.UserDao" />
    	</bean>
    </beans>

    the service code:
    Code:
    package com.test.service;
    
    import com.test.dao.UserDao;
    import com.test.entry.User;
    import com.test.interfaces.IService;
    
    public class UserService implements IService<User>  {
    
    	private UserDao userMapper;
    
    	public UserDao getUserMapper() {
    		return userMapper;
    	}
    
    	public void setUserMapper(UserDao userMapper) {
    		this.userMapper = userMapper;
    	}
    
    
    
    	public void save(User user) {
    		userMapper.saveUser(user);
    
    	}
    
    }


    the action code
    Code:
    	public User getUser() {
    		ApplicationContext acx = new ClassPathXmlApplicationContext(
    				"classpath:config/context.xml");
    		IService<User> us = acx.getBean("userService",IService.class);
    		User user = new User();
    		try {
    
    			user.setId("111");
    			
    			us.save(user);//step 1 success
    			us.save(user);//step 2 exception  but do not rollback
    			 
    			
    
    		} catch (Exception e) {
    			e.printStackTrace();
    
    		}
    
    		return user;
    	}
    Last edited by kingdom908; Jul 18th, 2010 at 12:37 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    1) You are using mysql, make sure you are using InnoDB tables and not MyISAM tables
    2) If you want to run out of resources your current approach (create a new instance of the ApplicationContext each time) that is the way to go. Inject the Service into your action, or use the WebApplicationContextUtils to retrieve an instance of the context.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2010
    Posts
    2

    Default

    Quote Originally Posted by Marten Deinum View Post
    1) You are using mysql, make sure you are using InnoDB tables and not MyISAM tables
    2) If you want to run out of resources your current approach (create a new instance of the ApplicationContext each time) that is the way to go. Inject the Service into your action, or use the WebApplicationContextUtils to retrieve an instance of the context.

    thank you for your reply.

    i changed the db to InnoDB and changed the code like blow:

    //add a context tool:
    Code:
    <bean id="ContextUtil" class="com.test.utils.ContextUtils" />
    Code:
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    public class ContextUtils implements ApplicationContextAware {
    
    
    	private static ApplicationContext applicationContext;
    
    	public void setApplicationContext(ApplicationContext applicationContext)
    			throws BeansException {
    		ContextUtils.applicationContext = applicationContext;
    	}
    
    	public static ApplicationContext getApplicationContext() {
    		return applicationContext;
    	}
    
    
    	public static<T> T  getBean(String beanId,Class<T> cls) throws BeansException {
    		return applicationContext.getBean(beanId, cls);
    	}
    }

    IService:

    Code:
    public interface IService<T> {
    
    	void save(T t);
    	
    }
    in the action:

    Code:
    //userService implements IService
    IService<User> us = ContextUtils.getBean("userService", IService.class);
    		User user = new User();
    		try {
    
    			user.setId("0001");
    			user.setName("apple");
    			us.save(user);// step 1
    			us.save(user);// step 2 exception but do not rollback
    
    		} catch (Exception e) {
    			e.printStackTrace();
    
    		}

    but still do not rollback

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    What would you expect? Step 1 is a transaction, when finished, commit, step 2 is a transaction, when finished commit. It isn't 1 transaction... Maybe a matter of expectation?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •