Grails:当前事务中的原始SQL查询 [英] Grails: Raw SQL query in the current transaction
本文介绍了Grails:当前事务中的原始SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用类似于
I am trying to execute a raw sql query using something similar to
def dataSource;
Sql sql = new Sql(dataSource);
但是,这似乎是在它自己的一个单独的事务中运行。因此,它忽略了在service方法中完成的所有(未提交的)更改。
But, it seems that this runs in a separate transaction of its own. It therefore misses all the (uncommitted) changes done before it in the service method.
在当前事务中运行原始sql查询的最佳方式是什么?
What is the best way to run a raw sql query in the current transaction?
推荐答案
上面的代码会创建一个新的连接,因此是一个新的事务。您可以使用当前的Hibernate会话(注入sessionFactory)在当前事务中执行原始sql,如下所示。
The above code would create a new connection, hence a new transaction. You can use the current Hibernate session(injecting sessionFactory) to execute a raw sql in the current transaction as below.
def session = sessionFactory.getCurrentSession()
def results = session.createSQLQuery(yourQueryString)
这篇关于Grails:当前事务中的原始SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文