forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent Oracle from committing uncommitted transactions on connection…
… close When a connection is closed, Oracle will commit the unfinished business, which is a very surprising behavior in most cases and could lead to data loss. I added a -Dquarkus-oracle-no-automatic-rollback-on-connection-close to disable this behavior if absolutely needed. If we have feedback that this is not something we should do always, we can add a proper configuration property (with a default value to determine). Fixes quarkusio#36265 Wouldn't have been possible without the nice work of @segersb and @Felk. Co-authored-by: Felix König <de.felix.koenig@gmail.com>
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...rc/main/java/io/quarkus/jdbc/oracle/runtime/RollbackOnConnectionClosePoolInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.quarkus.jdbc.oracle.runtime; | ||
|
||
import java.sql.Connection; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.agroal.api.AgroalPoolInterceptor; | ||
import oracle.jdbc.OracleConnection; | ||
|
||
/** | ||
* Oracle has the weird behavior that it performs an implicit commit on normal connection closure (even with auto-commit | ||
* disabled), | ||
* which happens on application shutdown. To prevent whatever intermittent state we have during shutdown from being committed, | ||
* we add an explicit rollback to each connection closure. If the connection has already received a COMMIT, the rollback will | ||
* not work, which is fine. | ||
* <p> | ||
* The code unwraps the {@link Connection} so that we perform the rollback directly on the underlying database connection, | ||
* and not on e.g. Agroal's {@link ConnectionWrapper} which can prevent the rollback from actually being executed due to some | ||
* safeguards. | ||
* | ||
* @see https://github.com/quarkusio/quarkus/issues/36265 | ||
*/ | ||
public class RollbackOnConnectionClosePoolInterceptor implements AgroalPoolInterceptor { | ||
|
||
private static final Logger LOG = Logger.getLogger(RollbackOnConnectionClosePoolInterceptor.class); | ||
|
||
private final boolean noAutomaticRollback; | ||
|
||
public RollbackOnConnectionClosePoolInterceptor() { | ||
// if you have to use this system property, make sure you open an issue in the Quarkus tracker to explain | ||
// why as we might need to adjust things | ||
noAutomaticRollback = Boolean.getBoolean("quarkus-oracle-no-automatic-rollback-on-connection-close"); | ||
} | ||
|
||
@Override | ||
public void onConnectionDestroy(Connection connection) { | ||
if (noAutomaticRollback) { | ||
return; | ||
} | ||
|
||
try { | ||
if (connection.unwrap(Connection.class) instanceof OracleConnection oracleConnection) { | ||
if (connection.isClosed() || connection.getAutoCommit()) { | ||
return; | ||
} | ||
|
||
oracleConnection.rollback(); | ||
} | ||
} catch (Exception e) { | ||
LOG.trace("Ignoring exception during rollback on connection close", e); | ||
} | ||
} | ||
} |