Java more than one DB connection in UserTransaction

前端 未结 5 788
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 23:12
static void clean() throws Exception {
  final UserTransaction tx = InitialContext.doLookup(\"UserTransaction\");
  tx.begin();

  try {
    final DataSource ds = In         


        
相关标签:
5条回答
  • 2021-01-19 23:34

    The only way to do this properly is to use a transaction manager and two phase commit XA drivers for all databases involved in this transaction.

    0 讨论(0)
  • 2021-01-19 23:35

    My guess is that you have autocommit enabled on the connections. This is the default when creating a new connection, as is documented here https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html

    System.out.println(connection1.getAutoCommit());
    

    will most likely print true.

    You could try

    connection1.setAutoCommit(false); 
    

    and see if that changes the behavior.

    In addition to that, it's not really defined what happens if you call close() on a connection and haven't issued a commit or rollback statement beforehand. Therefore it is strongly recommended to either issue one of the two before closing the connection, see https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#close()

    EDIT 1: If autocommit is false, the it's probably due to the undefined behavior of close. What happens if you switch the statements? :

    st2.executeUpdate();
    st2.close();
    connection2.close();
    st1.executeUpdate();
    st1.close();
    connection1.close();
    

    EDIT 2: You could also try the "correct" way of doing it:

    st1.executeUpdate();
    st1.close();
    st2.executeUpdate();
    st2.close();
    tx.commit();
    connection1.close();
    connection2.close();
    

    If that doesn't fail, then something is wrong with your setup for UserTransactions.

    0 讨论(0)
  • 2021-01-19 23:50

    Since your application is running in weblogic server, the java-EE-container is managing the transaction and the connection for you. If you call DataSource#getConnection multiple times in a java-ee transaction, you will get multiple Connection instances joining the same transaction. Usually those connections connect to database with the identical session. Using oracle you can check that with the following snippet in a @Stateless ejb:

    @Resource(lookup="jdbc/myDS")
    private DataSource ds;
    
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    @Schedule(hour="*", minute="*", second="42")
    public void testDatasource() throws SQLException {
    
        try ( Connection con1 = ds.getConnection();
              Connection con2 = ds.getConnection();
            ) {
    
            String sessId1 = null, sessId2 = null;
            try (ResultSet rs1 = con1.createStatement().executeQuery("select userenv('SESSIONID') from dual") ){
                if ( rs1.next() ) sessId1 = rs1.getString(1);
            };
            try (ResultSet rs2 = con2.createStatement().executeQuery("select userenv('SESSIONID') from dual") ){
                if ( rs2.next() ) sessId2 = rs2.getString(1);
            };
    
            LOG.log( Level.INFO," con1={0}, con2={1}, sessId1={2}, sessId2={3}"
                   , new Object[]{ con1, con2, sessId1, sessId2}
                   );
        }
    
    }
    

    This results in the following log-Message:

    con1=com.sun.gjc.spi.jdbc40.ConnectionWrapper40@19f32aa, 
    con2=com.sun.gjc.spi.jdbc40.ConnectionWrapper40@1cb42e0, 
    sessId1=9347407, 
    sessId2=9347407
    

    Note that you get different Connection instances with same session-ID.

    For more details see eg this question

    0 讨论(0)
  • 2021-01-19 23:51

    Depending on your database this is quite a normal case.

    An object implementing UserTransaction interface represents a "logical transaction". It doesn't always map to a real, "physical" transaction that a database engine respects.
    For example, there are situations that cause implicit commits (as well as implicit starts) of transactions. In case of Oracle (can't vouch for other DBs), closing a connection is one of them.

    From Oracle's docs:

    "If the auto-commit mode is disabled and you close the connection without explicitly committing or rolling back your last changes, then an implicit COMMIT operation is run".

    But there can be other possible reasons for implicit commits: select for update, various locking statements, DDLs, and so on. They are database-specific.

    So, back to our code.
    The first transaction is committed by closing a connection. Then another transaction is implicitly started by the DML on the second connection. It inserts non-conflicting changes and the second connection.close() commits them without PK violation. tx.commit() won't even get a chance to commit anything (and how could it? the connection is already closed).

    The bottom line: "logical" transaction managers don't always give you the full picture.
    Sometimes transactions are started and committed without an explicit reason. And sometimes they are even ignored by a DB.

    PS: I assumed you used Oracle, but the said holds true for other databases as well. For example, MySQL's list of implicit commit reasons.

    0 讨论(0)
  • 2021-01-19 23:53

    If auto-commit mode is disabled and you close the connection without explicitly committing or rolling back your last changes, then an implicit COMMIT operation is executed.

    Please check below link for details:

    http://in.relation.to/2005/10/20/pop-quiz-does-connectionclose-result-in-commit-or-rollback/

    0 讨论(0)
提交回复
热议问题