在grails中,我们有以下配置:
DataSource.groovy:
hibernate {
flush.mode="commit"
}
当我们在事务上下文中登录时会显示“COMMIT”:
println "session=${sessionFactory.currentSession.flushMode}"
但是当我们创建一个新线程时
这会打印“AUTO”。
新线程似乎确实获得了其他 hibernate 设置,即数据库,用户名和工厂,但是currentSession不采用flush.mode设置。
有人可以建议吗?
最佳答案
您在使用Quartz插件吗?
quartz 改变冲洗模式:
https://fisheye.codehaus.org/browse/~raw,r=41198/grails-plugins/grails-quartz/tags/LATEST_RELEASE/src/java/org/codehaus/groovy/grails/plugins/quartz/listeners/SessionBinderJobListener.java
public void jobToBeExecuted(JobExecutionContext context) {
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.AUTO);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
if( LOG.isDebugEnabled()) LOG.debug("Hibernate Session is bounded to Job thread");
}
解决方法是在作业中更改刷新模式:
def sessionFactory
.
.
.
def session=SessionFactoryUtils.getSession(sessionFactory, false)
session?.setFlushMode(FlushMode.COMMIT)
关于multithreading - grails sessionFactory.currentSession.flushMode无法与线程一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5316321/