0 0

Spring 2.5.5 @Transactional 的问题10

这个问题可能有点初级,要是看着不顺眼打新手贴吧。

在一个新项目里打算开始用Spring 2.5.5和大量使用annotation,可是@Transactional总是玩不转。

在applicationContext.xml里面:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource">
  </bean>
  <context:annotation-config>

  <context:component-scan base-package="se.posten.ida">
    <context:include-filter type="regex" expression=".*DAOImpl">
    <context:include-filter type="regex" expression=".*ServiceImpl">
  </context:component-scan>

  <tx:annotation-driven transaction-manager="transactionManager">


定义一个Business Interface:

public interface SomeService {
  void foobar();
}


实现类:

@Service("someService")
public class SomeServiceImpl implements SomeService {
  @Resource(name="someDAO")
  private SomeDAO someDAO;
  
  @Transactional (readOnly=false, isolation = Isolation.READ_COMMITTED)
  public void foobar() {
    someDAO.foobar();
  }
}


Web Controller

public class SomeController {
  @Resource(name="someService")
  private SomeService someService;
}


这里面用的是普通的JdbcTemplate, 结构是经典的Controller-Service-DAO.

换回经典的TransactionProxyFactoryBean, 工作的好好的。

我是不是什么地方忘配置了?

Classpath下面只有spring.jar 和 spring-mvc.jar,试了把 cglib-nodep-2.1_3.jar 拷贝过去也没用。

折腾了一天,实在黔驴技穷了。
问题补充:
比如说我这个business method 调用DAO两个方法,第一个成功了,第二个违反数据库的constraint,失败了,那么事务回滚应该把第一个DAO的更改rollback,但是这里@Transactional一点作用都没有,打开Spring的logging发现它根本就没调用任何和Transaction有关的Interceptor.

这里没有什么异常好贴的,比如我在business method里面,调用DAO方法后,写个throw new RuntimeException,那么如果事务配置好了,应该rollback, 但是这里Transaction一点作用没有。

如果把配置改成在XML里用TransactionProxyFactoryBean, 就工作了。

这个Transactional annotation就不工作。
问题补充:
实在不好意思,作为老会员,没有关心网站的新进展,前两天在Java论坛先发了个帖子问这个问题,发完了发现问题该发到问答频道去,于是又重发了一份,本来想发个短信给管理员删掉论坛帖子的,但当时已经半夜三点了,太困了,想第二天起来发,第二天起来管理员已经把论坛帖子移到问答频道去了,结果问答频道有两个同样的问题,

http://www.iteye.com/problems/2646

http://www.iteye.com/problems/2649

请大家去问题2646跟踪最新更新吧,特别感谢小疯子还自己写了程序试验。

我已经请求管理员删除这个2649号问题,把大家的回答搬到2646去。
2008年8月13日 09:05

3个答案 按时间排序 按投票排序

0 0

楼主提供的信息很乱, 不知道怎么看!不过我做了个简单的例子试了下, 没有发现楼主的问题, 很简单, 帖出来, 供楼主参考:

CREATE TABLE `user` (                    
          `id` int(11) NOT NULL auto_increment,  
          `name` varchar(64) NOT NULL,           
          PRIMARY KEY  (`id`)                    
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 

接口和实现:
public interface UserDao {
	void save(String name);
}
/*************************************
@Service
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {

	@Transactional(readOnly = false)
	public void save(String name) {
		getJdbcTemplate().execute("insert into user(name) values ('" + name + "')");
		// throw new IllegalStateException();//rollback
	}
}

service层:
@Service("service")
public class UserService {

	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void exe() {
		userDao.save("fuxueliang");
	}

}

spring配置:
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byType">
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="minPoolSize" value="5" />
		<property name="idleConnectionTestPeriod" value="3600" />
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/javaeye?useUnicode=true&amp;characterEncoding=UTF-8" />
		<property name="user" value="root" />
		<property name="password" value="root" />
	</bean>
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
	<context:component-scan base-package="com.rondy.application.jdbc">
		<context:include-filter type="regex" expression=".*DaoImpl" />
		<context:include-filter type="regex" expression=".*Service" />
	</context:component-scan>
</beans>

测试类:
public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/rondy/application/jdbc/application.jdbc.xml");
		UserService service = (UserService) applicationContext.getBean("service");
		service.exe();
	}

楼主可以修改注释掉的exception部分,看看是不是rollback了.

2008年8月13日 10:57
0 0

这个问题我好像见过
http://www.iteye.com/problems/2646

2008年8月13日 09:48
0 0

出什么异常啊,贴出来看咯

2008年8月13日 09:05

相关推荐

Global site tag (gtag.js) - Google Analytics