开发者

GORM (Hibernate) Interceptor to run certain SQL before Hibernate runs it in the db in a Grails application?

I have a grails application in which I am using GORM. This works great. However I have a requirement where, before any SQL is called in the database,开发者_JAVA技巧 it has to run a certain stored procedure. So, is there a way I can do something that will trigger my method that kicks off a stored procedure before Hibernate runs the SQL for select, insert, update, delete etc. Your response will be greatly appreciated.

(P.S.- The reason I have to run certain stored procedure is to change Oracle Workspace)


There might be several options.

You could use a dataSource wrapper that extends org.springframework.jdbc.datasource.DelegatingDataSource and runs the statement whenever getConnection is called. I guess it's ok to call the statement (stored proc) only once per transaction, so DelegatingDataSource is probably the most approriate solution.

You could also use http://www.grails.org/plugin/jdbc-pool plugin (wraps Tomcat JDBC Pool) and try to hook to the datasource pool implementation. Tomcat JDBC Pool supports interceptors.

It's also possible to modify the SQL sent by GORM/Hibernate using a Hibernate interceptor, if that helps: Is it possible to map a table name for a domain object dynamically in grails? .


It's not clear whether you want to run this stored proc once before any SQL is executed, or before every SQL statement is executed. Here's a suggestion for both cases:

Once Only

Call the method that invokes the stored proc in Bootrap.init()

Before Every

Call the method from the before* GORM event handlers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜