spring3整合hibernate3做声明式事务管理时,人为抛出throw RuntimeException时,事务继续提交,没有回滚。
具体代码如下:测试类publicclassUserServiceTest{@TestpublicvoidtestAdd()throwsException{Applicat...
具体代码如下:
测试类
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = ctx.getBean("userService",UserService.class);
User user = ctx.getBean("user",User.class);
user.setUsername("user");
user.setPassword("pwd");
Log log = ctx.getBean("log",Log.class);
log.setMsg("msg");
service.add(user,log);
}
}
service类
package org.spring.aop.service;
import java.util.Date;
import org.spring.aop.dao.LogDAO;
import org.spring.aop.dao.UserDAO;
import org.spring.aop.model.Log;
import org.spring.aop.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Component("userService")
public class UserService {
private UserDAO userDao;
private LogDAO logDao;
public UserDAO getUserDao() {
return userDao;
}
@Resource(name="userDAO")
public void setUserDao (UserDAO userDao) {
this.userDao = userDao;
}
public LogDAO getLogDao() {
return logDao;
}
@Resource(name="logDAO")
public void setLogDao(LogDAO logDao) {
this.logDao = logDao;
}
@Transactional(rollbackFor=RuntimeException.class)
public void add(User user,Log log){
logDao.save(log);
userDao.save(user);
}
}
beans.xml文件
<context:annotation-config />
<context:component-scan base-package="org.spring.aop" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>org.spring.aop.model.User</value>
<value>org.spring.aop.model.Log</value>
</list>
</property>
<property name="hibernateProperties"> <props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>
package org.spring.aop.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.aspectj.lang.annotation.AfterThrowing;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.spring.aop.dao.UserDAO;
import org.spring.aop.model.User;
import org.springframework.stereotype.Component;
@Component("userDAO")
public class UserDAOImpl implements UserDAO {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(User user) {
Session s = sessionFactory.getCurrentSession();
s.save(user);
}
} 展开
测试类
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = ctx.getBean("userService",UserService.class);
User user = ctx.getBean("user",User.class);
user.setUsername("user");
user.setPassword("pwd");
Log log = ctx.getBean("log",Log.class);
log.setMsg("msg");
service.add(user,log);
}
}
service类
package org.spring.aop.service;
import java.util.Date;
import org.spring.aop.dao.LogDAO;
import org.spring.aop.dao.UserDAO;
import org.spring.aop.model.Log;
import org.spring.aop.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Component("userService")
public class UserService {
private UserDAO userDao;
private LogDAO logDao;
public UserDAO getUserDao() {
return userDao;
}
@Resource(name="userDAO")
public void setUserDao (UserDAO userDao) {
this.userDao = userDao;
}
public LogDAO getLogDao() {
return logDao;
}
@Resource(name="logDAO")
public void setLogDao(LogDAO logDao) {
this.logDao = logDao;
}
@Transactional(rollbackFor=RuntimeException.class)
public void add(User user,Log log){
logDao.save(log);
userDao.save(user);
}
}
beans.xml文件
<context:annotation-config />
<context:component-scan base-package="org.spring.aop" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>org.spring.aop.model.User</value>
<value>org.spring.aop.model.Log</value>
</list>
</property>
<property name="hibernateProperties"> <props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>
package org.spring.aop.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.aspectj.lang.annotation.AfterThrowing;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.spring.aop.dao.UserDAO;
import org.spring.aop.model.User;
import org.springframework.stereotype.Component;
@Component("userDAO")
public class UserDAOImpl implements UserDAO {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(User user) {
Session s = sessionFactory.getCurrentSession();
s.save(user);
}
} 展开
展开全部
你的DAO就没有配置到事务里面嘛,请仔细检查!!
追问
我采用的是注解的方式
@Transactional(rollbackFor=RuntimeException.class)
public void add(User user,Log log){
logDao.save(log);
userDao.save(user);
}
这里不是已经把我需要添加事务的方法,加入了事务了吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询