What happens if we start a transaction in sleep mode but don’t commit it?

What happens if we start a transaction in sleep mode and then make some transaction but don’t commit it? Does he keep his temperament or will he roll back immediately?

Thanks Chetan

+4
source share
2 answers

Look at the following code that accesses a database with transaction boundaries without using commit:

Session session = sessionFactory.openSession();
session.beginTransaction(); 
session.get(Item.class, 123l); 
session.close(); 

By default in a Java SE environment with JDBC configuration, this happens if you execute this snippet:

  • A new session opens. It does not receive a database connection at this point.
  • , .
  • get() SQL SELECT.    JDBC . Hibernate, ,      setAutoCommit (). JDBC!
  • SELECT JDBC. , Hibernate - Hibernate close() JDBC.

    ?

: " !" JDBC , close() . , , . , Oracle JDBC close() ! JDBC , JDBC .

, SELECT, , :

Session session = getSessionFactory().openSession(); 
session.beginTransaction();
Long generatedId = session.save(item); 
session.close(); 

INSERT, , . Oracle ; . ( : INSERT , . , INSERT. , . .)

+4

hibernate-config .

- ( , - hibernate.ejb.discard_pc_on_close true)

public void close() {
    if ( !open ) {
        throw new IllegalStateException( "EntityManager is closed" );
    }
    if ( !discardOnClose && isTransactionInProgress() ) {

, discard_pc_on_close, hibernate - ( ), , . c3p0 : NewPooledConnection. , - FORCE_IGNORE_UNRESOLVED_TXNS ( - false), reset .

static void resetTxnState( Connection pCon, 
               boolean forceIgnoreUnresolvedTransactions, 
               boolean autoCommitOnClose, 
               boolean txnKnownResolved ) throws SQLException
{
if ( !forceIgnoreUnresolvedTransactions && !pCon.getAutoCommit() )
0

All Articles