public Transaction begin ( ) throws NotSupportedException , SystemException , RollbackException { // check if there isn't an active transaction already NestableThreadLocalTransaction localTx = LOCAL_TRANSACTION . get ( ) ; if ( localTx != null ) { // we have an existing local transaction so we need to be aware of nesting by calling 'begin' if ( logger . isTraceEnabled ( ) ) { logger . trace ( "Found active ModeShape transaction '{0}' " , localTx ) ; } return localTx . begin ( ) ; } // Get the transaction currently associated with this thread (if there is one) ... javax . transaction . Transaction txn = txnMgr . getTransaction ( ) ; if ( txn != null && Status . STATUS_ACTIVE != txn . getStatus ( ) ) { // there is a user transaction which is not valid, so abort everything throw new IllegalStateException ( JcrI18n . errorInvalidUserTransaction . text ( txn ) ) ; } if ( txn == null ) { // There is no transaction or a leftover one which isn't active, so start a local one ... txnMgr . begin ( ) ; // create our wrapper ... localTx = new NestableThreadLocalTransaction ( txnMgr ) ; localTx . begin ( ) ; // notify the listener localTx . started ( ) ; return logTransactionInformation ( localTx ) ; } // There's an existing tx, meaning user transactions are being used SynchronizedTransaction synchronizedTransaction = transactionTable . get ( txn ) ; if ( synchronizedTransaction != null ) { // notify the listener synchronizedTransaction . started ( ) ; // we've already started our own transaction so just return it as is return logTransactionInformation ( synchronizedTransaction ) ; } else { synchronizedTransaction = new SynchronizedTransaction ( txnMgr , txn ) ; transactionTable . put ( txn , synchronizedTransaction ) ; // and register a synchronization txn . registerSynchronization ( synchronizedTransaction ) ; // and notify the listener synchronizedTransaction . started ( ) ; return logTransactionInformation ( synchronizedTransaction ) ; } }
Starts a new transaction if one does not already exist and associate it with the calling thread .