相关文章
更多最近更新
更多spring 事务没有回滚
2019-03-25 13:42|来源: 网路
我是学习spring新手, 碰到spring事务没有回滚的问题, 希望知道原因的指教,不胜感激:
我在mysql里建了一个测试的admin表(innoDB 类型), 只有id一个字段(整形)。
4条数据:
id
1
2
3
4
配置文件:
Struts Action 类:
Server 类:
DAO 类:
我在mysql里建了一个测试的admin表(innoDB 类型), 只有id一个字段(整形)。
4条数据:
id
1
2
3
4
配置文件:
<bean name="/system/branch_modi" class="com.jandar.fdpweb.action.system.BranchOperationAction"> <property name="branchManage"> <ref bean="branchManage"/> </property> </bean> <bean id="branchManage" class="com.jandar.fdpweb.service.system.BranchManage"> <property name="branchTransaction"> <ref bean="branchTransaction"/> </property> </bean> <bean id="branchTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="myTransactonManager"/> </property> <property name="target"> <ref local="branchDao"/> </property> <property name="transactionAttributes"> <props> <prop key="deleteTest">PROPAGATION_REQUIRED, -Exception</prop> </props> </property> </bean> <bean id="branchDao" class="com.jandar.fdpweb.dao.jdbc.system.BranchDaoJdbcImpl"> <property name="jdbcTemplate"> <ref bean="jdbcTemplate" /> </property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="c3p0DataSource" /> </property> </bean> <bean id="myTransactonManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="c3p0DataSource"/> </property> </bean> <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>com.mysql.jdbc.Driver</value> </property> <property name="jdbcUrl"> <value>jdbc:mysql://localhost:3306/cjr_test?characterEncoding=utf8</value> </property> <property name="user"> <value>root</value> </property> <property name="password"> <value>123456</value> </property> <property name="initialPoolSize"><value>10</value></property> <property name="minPoolSize"><value>5</value></property> <property name="maxPoolSize"><value>20</value></property> <property name="acquireIncrement"><value>5</value></property> <property name="maxIdleTime"><value>60</value></property> <property name="maxStatements"><value>0</value></property> </bean>
Struts Action 类:
public class BranchOperationAction extends DispatchAction { private BranchManage branchManage; public BranchManage getBranchManage() { return branchManage; } public void setBranchManage(BranchManage branchManage) { this.branchManage = branchManage; } public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ActionForward forward = new ActionForward(); DynaActionForm modifyForm = (DynaActionForm) form; String branchId = modifyForm.getString("branch_tj").trim(); branchManage.deleteTest(branchId); forward = mapping.findForward("success"); return (forward); } }
Server 类:
public class BranchManage { private BranchDao branchTransaction; public BranchDao getBranchTransaction() { return branchTransaction; } public void setBranchTransaction(BranchDao branchTransaction) { this.branchTransaction = branchTransaction; } public void deleteTest(String branchId) { branchTransaction.deleteBranch(branchId); } }
DAO 类:
public interface BranchDao { public void deleteBranch(final String branchId); } public class BranchDaoJdbcImpl implements BranchDao { private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void deleteBranch(final String branchId) { String sqlDelete1 = "delete from admin where id = 1"; // 先删除一条记录 int[] arr = {1, 2, 3, 4}; jdbcTemplate.update(sqlDelete1); //我调试时, 程序运行这行后, 数据库居然删了一行, 事务这时应该没提交,不会删数据库才对啊, 是这样吗? //数组下标越界,想让程序抛出Runtime Exception String sqlDelete2 = "delete from admin where id = " + arr[5]; jdbcTemplate.update(sqlDelete2); // 删除第二条报错,事务没有回滚 } }
相关问答
更多-
是不是有一款什么手机软件,可以很自然的拉长腿部?不是美图秀秀,有知道的不?[2022-10-20]
spring -
spring3 不能回滚事务[2023-01-15]
配置没错!如果你是test方法中 先调用updateUser方法正确,然后继续调用saveUser方法 报主键已存在错误。updateUser方法 事务是不会回滚的。你需要把控制层的test方法写到service层去,比如saveTest方法。然后在控制层test方法中调用saveTest。 这样updateUser和saveUser才是一个整体。出错整体回滚。 -
春天的英语怎么读?[2022-05-21]
spring -
spring中事务回滚是什么意思[2022-06-23]
首先 事物不是spring独有的,这是数据库的标准。 事物:就是说好比你操作了a、b、c三张表,操作了前2张表没问题数据已经改变,但是到第三章表的时候出了问题,这时候你想撤销a、b两张表的操作。以保证数据的一致性。这时候就要用的事物的回滚。 事物回滚在所有的数据库操作里面都有。 spring对事物做了封装。目前有5种方式,这里就不说了,你在去查下资料吧。 回了这么多给个最佳答案吧。 -
spring测试事务回滚,抛出RuntimeException[2021-11-18]
throw new RuntimeException("error!"); 按道理说这里运行到这里应该抛出异常。如果你建好了entry实体类。数据库中表应该创建好了。 但是运行到这里throw new RuntimeException("error!"); 你save 数据不能进去就可以成功。你看看你数据库表里面有没有数据。 你的junit 有没有执行 service.add(new User()); 这个方法,如果你没有执行它就不会抛出异常 public void add(User user) { use ... -
Spring 事务回滚问题[2021-12-21]
你的应用使用了两层体系,将业务都在DAO中组装了。而你的DAO中出现了对其它数据库操作的调用。而这些调用本身也在事务控制,所以执行後就已经提交了。解决方式是,对于事务嵌套调用需要配置嵌套事务。 -
spring中事务回滚,如何设置?[2024-01-12]
单写一个接口,接口中再定义提交和回滚,在接口中判断。 比如有两个sql语句,第一个执行了后,判断是否有异常、错误。如果第一个出现异常等,那么直接就停止了。 若第一个sql执行通过了,第二个报错。那么你调用接口中的回滚就可以了。 好久不做开发了。详细代码没法说。也许说的不怎么对。你验证一个。当然思路基本就这样的。 -
spring事务不能回滚[2022-03-31]
当然不能回滚啦,因为你要把UserService us = (UserService)act.getBean("UserService"); User user1 = new User(); user1.setUsername("cba"); user1.setPassword("xyzxyzxyzxyz"); User user = new User(); user.setUsername("abc"); user.setPassword("xyz"); us.save(user); us.save(us ... -
请问各位java高手,spring mvc 事务提交无法回滚的问题如何解决啊?[2023-03-22]
正常啊,service层就是用来管理事务的 如果service层没有抛异常,则事务提交 如果抛出异常,则spring会回滚事务 这个就是spring aop -
Spring回滚事务部分(Spring rollback transaction part)[2022-06-19]
我用annotaion @Transactional(propagation = Propagation.REQUIRES_NEW)为cretePartA()方法解决了问题,并尝试了/ catch块 I solved it with annotaion @Transactional(propagation = Propagation.REQUIRES_NEW) for cretePartA() method and try/catch block