spring事务不能回滚?

QWERT4745 2010-11-20 04:52:46
xml:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<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>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>model.User</value>
<value>model.Log</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="current_session_context_class">thread</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"></property>
</bean>

<aop:config>
<aop:pointcut expression="execution(public * service..*.*(..))"
id="bussinessService" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="getUser" read-only="true" rollback-for="RuntimeException"/>
<tx:method name="add*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>


</beans>

userService.java
@Component
public class UserService {
private LogDAO logDAO;
private UserDAO userDAO;

public void add(User u) {
this.userDAO.save(u);
Log log = new Log();
log.setMsg("a used saved!!");
logDAO.save(log);

}

public LogDAO getLogDAO() {
return logDAO;
}

public UserDAO getUserDAO() {
return userDAO;
}

@Resource
public void setLogDAO(LogDAO logDAO) {
this.logDAO = logDAO;
}

@Resource
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}

LogDAOImpl.java
@Component
public class LogDAOImpl implements LogDAO {
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public void save(Log log) {

Session s = sessionFactory.getCurrentSession();
s.save(log);
throw new RuntimeException("error!"); //这里明明抛出了运行时异常,但数据还是插进去了

}

}

事务无法回滚,用注解的也一样,我是看的一个老师的视频敲的代码,我把老师的源码copy进去运行,也还是不行,不知是什么原因啊
...全文
317 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
QWERT4745 2010-11-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tansonghui 的回复:]
不知道你的数据库表是不是你的数据库表支不支持事务回滚,如果你的数据库用的是MYSQL,默认建的表是不支持事务的,
[/Quote]
谢谢,问题已经解决了,我用的mysql默认表的类型是MyISAM,不支持事务,改了类型之后就OK了
songTan08 2010-11-20
  • 打赏
  • 举报
回复
不知道你的数据库表是不是你的数据库表支不支持事务回滚,如果你的数据库用的是MYSQL,默认建的表是不支持事务的,
futrueing 2010-11-20
  • 打赏
  • 举报
回复
好长,看到就怕了~
QWERT4745 2010-11-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gxm0751129 的回复:]
你都已经save了,数据都已经被你存到数据库里了,你再去抛异常有什么用
[/Quote]

事务回滚不应该是在出现异常之后回到未修改的状态么,即使save了,也可以撤销对数据库的更新哪
marqio 2010-11-20
  • 打赏
  • 举报
回复
<tx:method name="getUser" read-only="true" rollback-for="RuntimeException"/>
<tx:method name="add*" propagation="REQUIRED" />
加上<tx:method name="save*" propagation="REQUIRED" />
舞黯然 2010-11-20
  • 打赏
  • 举报
回复
你都已经save了,数据都已经被你存到数据库里了,你再去抛异常有什么用
QWERT4745 2010-11-20
  • 打赏
  • 举报
回复
但实验结果是, 事务没有回滚,数据本不应该插进表的但插进去了,不晓得代码哪里错了还是怎样
moon&sean 2010-11-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ma1kong 的回复:]

在Spring中失误是可以回滚的。
Java code

public void save(Log log) {

Session s = sessionFactory.getCurrentSession();
s.save(log);
throw new RuntimeException("error!"); //这里明明抛出了运行时异常,但数据还是插进去了

}


你这个代码不……
[/Quote]
上面打错了两个字,失误=>事务
在Spring中事务是可以回滚的。
moon&sean 2010-11-20
  • 打赏
  • 举报
回复
在Spring中失误是可以回滚的。

public void save(Log log) {

Session s = sessionFactory.getCurrentSession();
s.save(log);
throw new RuntimeException("error!"); //这里明明抛出了运行时异常,但数据还是插进去了

}

你这个代码不就是成功插入数据之后抛异常吗?
你的运行结果是正确的

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧