简体   繁体   English

连接关闭后的JDBC RollBack

[英]JDBC RollBack After Connection Close

Say a connection is opened and auto commit is set to false to start a transaction. 假设连接已打开且自动提交设置为false以启动事务。 After the transaction is complete, I would like to rollback any changes to bring the database to the state it was before I started the transaction. 在事务完成之后,我想回滚任何更改以使数据库进入我开始事务之前的状态。

My problem is, that during the transaction the connection is closed, and thus all the changes are committed at that time. 我的问题是,在事务期间连接被关闭,因此所有更改都在那时提交。

Is there a way to rollback the state of the database to before the initial transaction started? 有没有办法在初始事务开始之前回滚数据库的状态?

and yes I've tried using Savepoints to no avail. 是的,我尝试使用Savepoints无济于事。

This really depends on what you're doing, why and how. 这实际上取决于你在做什么,为什么以及如何做。

  • If you have a reference to the transaction, you can rollback yourself. 如果您有对事务的引用,则可以自行回滚。
  • If not, can you throw an exception at the end? 如果没有,你可以在最后抛出一个例外吗?
  • If all you want is to test things, why not use an embedded DB like H2 and start from scratch each time? 如果您只想测试一下,为什么不使用像H2这样的嵌入式数据库并且每次都从头开始?
  • If H2 doesn't cut it, check out testcontainers . 如果H2没有削减它,请检查testcontainers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM