简体   繁体   English

如何确保数据库事务只发生一次?

[英]How to make sure that DB transaction happens only one time?

How to make sure that a particular DB transaction happens only once. 如何确保特定数据库事务只发生一次。 I am making a payment request from my mobile (more than once), but the backend should only execute only one. 我正在通过我的手机发送付款请求(不止一次),但后端应该只执行一次。 Once the request is executed its status is marked as COMPLETED. 执行请求后,其状态将标记为COMPLETED。 But in case of multiple request, before one request gets completed another starts is execution so the payment is done twice before the status to be marked COMPLETED. 但是在多个请求的情况下,在一个请求完成之前,另一个启动是执行,因此在状态被标记为COMPLETED之前完成两次付款。 How to solve this problem? 如何解决这个问题呢? I am using Java as backend. 我使用Java作为后端。 How can synchronize() help to solve this problem? synchronize()如何帮助解决这个问题?

You can try to add a lock around the code. 您可以尝试在代码周围添加锁定。 That way only one thread can enter at any given time. 这样,在任何给定时间只能有一个线程进入。 If you make one request, then the other request have to wait until the request is finish. 如果您发出一个请求,那么另一个请求必须等到请求完成。

This is a known issue as Double Post. 这是Double Post的已知问题。

Preventing parallel access to the method with Synchronize and lock will not help you, as the requests will be proceed in series. 使用Synchronize和lock阻止对方法的并行访问对您没有帮助,因为请求将按顺序进行。

Using client Side methods may help, but is not enough, as many things may happen at client side. 使用客户端方法可能有所帮助,但还不够,因为客户端可能会发生许多事情。

If you want to prevent it at Server Side (this is the correct way to do), you can add a hidden field to the client form (some unique hash string) and send it to the server with every request. 如果要在服务器端阻止它(这是正确的方法),您可以向客户端表单添加一个隐藏字段(一些唯一的哈希字符串),并在每次请求时将其发送到服务器。 In the Server side component, you can check if a request with that hash is already received, and if so, return an error code to client. 在服务器端组件中,您可以检查是否已收到具有该哈希的请求,如果是,则将错误代码返回给客户端。

You can also persist the hash with your Data and make it a unique field, so the first request that reach your database will be persisted, and the others will see unique field errors. 您还可以将哈希值与数据一起保留并使其成为唯一字段,因此到达数据库的第一个请求将被保留,其他请求将看到唯一的字段错误。

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

相关问题 如何确保一次只调用一个servlet(不是实例)? - how to I make sure that only one servlet(not instance) is call at a time? 如何确保仅按下一个键 - How to make sure only one key is pressed 春季事务回滚仅在第一次发生 - Spring transaction rollback happens only first time 如何确保我的方法只询问一次文件名并向其中添加对象 - How to make sure my method only asks for a file name one time and adds objects into it 如何确保休眠状态已提交事务? - how to make sure that hibernate committed a transaction? 如何确保在 Java 中只选择了一个 JRadioButton? - How Do I Make Sure Only One JRadioButton Is Selected In Java? 如何确保集合仅包含接口的每个实现之一 - How to make sure a collection only contains one of each of the implementations of an interface 如何确保仅启动一个线程 - How do I make sure only one thread gets started 如何确保java中的Collections只包含一个Type? - How to make sure that Collections in java hold only one Type? 我们如何确保在任何时间仅调用类的静态方法和非静态方法之一? - How can we make sure that at any point of time only one of a static method & a non-static method of a class gets called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM