【发布时间】:2010-10-12 17:58:28
【问题描述】:
好的,够了。我无法完成这项工作。我是 Spring 事务的新手,并在我的服务中使用 @Transactional 注释来管理事务。下面是我的spring bean配置文件。
<?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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myapp"/>
<!-- other <bean/> definitions here -->
</beans>
我为我的服务添加注释:
@Transactional
public class MyServiceImpl implements MyService {
...
}
我注意到两件事
- 我在 DAO [使用 DataSourceUtils.getConnection(dsName)] 中获得的连接启用了自动提交 [true]。
- 据我调试,在我的服务方法调用期间似乎没有任何事务已开始。
有人遇到过这个问题吗?
【问题讨论】:
-
只是为了确保:您的
MyServiceImpl是 Spring bean,而不是由new创建的,是吗? -
是的,它是通过 Spring 注入的
-
santhakr:另一个问题 - 您的
MyServiceImpl是否在与<tx:annotation-driven>相同的应用程序上下文中声明? -
您是否有一些 xml 用于扫描类以查找注释?
-
如果 MyServiceImpl 是由 spring 注入的,它在哪里定义?它不在上面的文件中...
标签: java spring transactions