简体   繁体   English

在Hibernate事务之外更改Oracle会话变量

[英]Alter Oracle session variables outside of a Hibernate transaction

We've been using a pattern like this for a while to ensure a specific operation is executed with BATCH NOWAIT , for performance reasons. 出于性能原因,一段时间以来,我们一直在使用这种模式来确保使用BATCH NOWAIT执行特定操作。

try {
    session.createSQLQuery("ALTER SESSION SET COMMIT_LOGGING='BATCH' COMMIT_WAIT='NOWAIT'").executeUpdate();
    // Do the operation (which also calls transaction.commit())
    return callback.apply(session);
} finally {
    session.createSQLQuery("ALTER SESSION SET COMMIT_LOGGING='IMMEDIATE' COMMIT_WAIT='WAIT'").executeUpdate();
}

This has worked fine in Hibernate 4. As of Hibernate 5, the last statement fails because it's not inside a transaction (as it's just been committed). 在Hibernate 4中,此方法运行良好。从Hibernate 5开始,最后一条语句失败了,因为它不在事务内(因为它刚刚被提交)。

javax.persistence.TransactionRequiredException: Executing an update/delete query javax.persistence.TransactionRequiredException:执行更新/删除查询

It isn't an update or a delete, but executeUpdate() is the only method you can call to execute this statement without returning any rows. 它不是更新或删除,但是executeUpdate()是可以调用以执行此语句而不返回任何行的唯一方法。 It shouldn't need to be in a transaction since session variables apply to the entirety of the connection, and it does need to be executed to restore the session variables because a connection pool is in use. 由于会话变量适用于整个连接,因此它不必处于事务中,并且由于连接池正在使用中,因此必须执行该操作以还原会话变量。

I've tried using one of the query methods instead, but this statement has -1 rows, and it won't let me stack SELECT 1 FROM DUAL on the end. 我尝试使用一种查询方法代替,但是该语句有-1行,并且不会让我在最后堆叠SELECT 1 FROM DUAL

Is there any way to execute a native query from Hibernate that's neither update/delete or results-returning, outside of a transaction? 有没有什么办法可以从Hibernate中执行既不在事务之外也不进行更新/删除或返回结果的本地查询呢?

Using the underlying Connection directly bypasses Hibernate's checks and allows me to execute such a statement in peace. 使用基础连接直接绕过了Hibernate的检查,并允许我安心地执行这样的语句。

try {
    session.doWork(conn ->
            conn.createStatement().execute("ALTER SESSION SET COMMIT_LOGGING='BATCH' COMMIT_WAIT='NOWAIT'")
    );
    return callback.apply(session);
} finally {
    session.doWork(conn ->
            conn.createStatement().execute("ALTER SESSION SET COMMIT_LOGGING='IMMEDIATE' COMMIT_WAIT='WAIT'")
    );
}

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

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