【发布时间】:2015-06-17 19:30:20
【问题描述】:
我使用了一些教程中的以下代码:
public class Main
{
public static void main( String[] args )
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
OuterBean testBean = (OuterBean) ctx.getBean("outerBeanImpl");
User user = new User();
user.setUsername("johndoe");
user.setName("John Doe");
try{
testBean.testRequired(user);
} catch(Exception e){
System.out.println("in main class");
e.printStackTrace();
}
}
}
OuterBeanImpl 类
@Service
public class OuterBeanImpl implements OuterBean {
@Autowired
private TestDAO testDAO;
@Autowired
private InnerBean innerBean;
@Override
@Transactional(propagation=Propagation.REQUIRED)
public void testRequired(User user) {
testDAO.insertUser(user);
try{
innerBean.testRequired();
} catch(RuntimeException e){
System.out.println("OuterBeanImpl class");
}
}
@Override
@Transactional(propagation=Propagation.REQUIRED)
public void testRequiresNew(User user) {
testDAO.insertUser(user);
try{
innerBean.testRequiresNew();
} catch(Exception e){
// handle exception
}
}
}
@Service
public class InnerBeanImpl implements InnerBean {
@Override
@Transactional(propagation=Propagation.REQUIRED)
public void testRequired() {
System.out.println("Rollback this required transaction!");
throw new RuntimeException("Rollback this required transaction!");
}
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void testRequiresNew() {
System.out.println("Rollback this new transaction!");
throw new RuntimeException("Rollback this new transaction!");
}
}
我的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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.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.xsd">
<tx:annotation-driven />
<context:component-scan
base-package="com.byteslounge.spring.tx.dao.impl" />
<context:component-scan
base-package="com.byteslounge.spring.tx.test.impl" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.byteslounge.spring.tx.model" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
</beans>
由于两个 bean 中的方法 testRequired() 都被标记为必需,因为内部事务抛出异常,外部事务必须推出该事务。但这不是在这里发生的。 谁能建议我做错了什么?
谢谢,
【问题讨论】:
-
您使用的是哪个 transactionManager?
-
@Massimo:
-
@Massimo:仅供参考,我指的是以下教程:byteslounge.com/tutorials/…
-
您是否尝试过使用 JtaTransactionManager?就像这里表达的那样:docs.spring.io/spring/docs/current/spring-framework-reference/… 我猜 HibernateTM 不支持这种传播技巧,但我可能会弄错
-
你是否也指定了
?
标签: spring transactions spring-transactions