简体   繁体   English

在Java连接上执行自动提交时如何获得通知?

[英]How to get notified when auto-commit is performed on a Java connection?

I'm trying to figure out if there's a way to register to an auto-commit event. 我试图找出是否有一种方法可以注册到自动提交事件。

Is there an observer? 有观察员吗? Perhaps I decorate an auto-commit connection or statement in order to be notified? 也许我装饰了一个自动提交的连接或语句以便得到通知? What is the method to decorate? 装饰的方法是什么?

I tried listening to Connection.commit() but it's not being invoked on statement executions. 我试听了Connection.commit()但没有在语句执行时调用它。

Thanks. 谢谢。 Alik. 阿利克。

auto-commit means that there is an implied commit at the end of each statement execution. 自动提交意味着在每个语句执行结束时都有一个隐式提交。 As far as I know there is no event that you can listen to, and you cannot expect anyone to internally invoke Connection.commit() , because the auto-commit happens somewhere within the stack of the RDBMS that you are using, possibly even on the server. 据我所知,没有事件可以监听,您不能期望任何人在内部调用Connection.commit() ,因为自动提交发生在您正在使用的RDBMS堆栈中的某个位置,甚至可能在服务器。

So, in order to add an auto-commit event to JDBC you have to decorate every single function that may cause a statement to be executed, and see whether the SQL that was executed started with UPDATE , INSERT or DELETE verb. 因此,为了向JDBC添加自动提交事件,您必须修饰可能导致语句执行的每个函数,并查看执行的SQL是否以UPDATEINSERTDELETE动词开头。 (Basically, anything but SELECT .) (基本上是SELECT任何东西。)

The way I have decorated JDBC in the past is as follows: 我过去装饰JDBC的方式如下:

  1. Make a copy of every single JDBC interface, create a class out of it, create a concrete function for each method which delegates to a reference to a JDBC interface. 为每个JDBC接口创建一个副本,从中创建一个类,为每个方法创建一个具体的函数,该方法委托给JDBC接口的引用。 Use lots of search-and-replace or possibly an editor that supports recording and replaying macros for that. 使用大量搜索和替换,或者使用支持该宏的录制和重放宏的编辑器。

  2. Define your own driver, and register it with JDBC, using your own driver name, like "jdbcdeco". 定义您自己的驱动程序,并使用您自己的驱动程序名称(例如“ jdbcdeco”)向JDBC注册。

  3. Establish the following convention: the connection string of your driver will be "jdbcdeco:" followed by the connection string of the driver that you want to decorate. 建立以下约定:驱动程序的连接字符串为"jdbcdeco:"后跟要装饰的驱动程序的连接字符串。 So, if the connection string of the connection that you want to decorate is "jdbc:mysql://localhost/test" then the following connection string must be specified: "jdbc:jdbcdeco:mysql://localhost/test" 因此,如果要装饰的连接的连接字符串是"jdbc:mysql://localhost/test"那么必须指定以下连接字符串: "jdbc:jdbcdeco:mysql://localhost/test"

  4. JDBC will instantiate your driver, and it will pass it the connection string, which begins with "jdbcdeco:" . JDBC将实例化您的驱动程序,并将其传递给连接字符串,该字符串以"jdbcdeco:"开头。 You strip away the "jdbcdeco:" part, and you are left with another connection string, which you use to pass again to JDBC in order to create the actual connection that is going to be decorated. 除去"jdbcdeco:"部分,然后剩下另一个连接字符串,可使用该字符串再次将其传递给JDBC,以创建将要装饰的实际连接。

Good luck, and have fun! 祝好运并玩得开心点!

Short answer- no such way exists to register to hear on auto commit. 简短的答案-不存在注册自动提交时可以收听的方式。 As mentioned in the answer by Mike, if you want to capture every commit you need to write custom code, you have to make the call whether you want to go through that much effort or not. 正如Mike的答案中所提到的,如果您想捕获编写自定义代码所需的每个提交,则无论是否要付出很多努力,都必须进行调用。

A simpler workaround would be to setAutoCommit as off on your connection but that is not what you have asked for :). 一个更简单的解决方法是在连接上将autoAutoCommit设置为off,但这不是您要的:)。

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

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