public < V > V runInTransaction ( Callable < V > operation , int retryCountOnLockTimeout , String ... keysToLock ) { // Start a transaction ... Transactions txns = repoEnv . getTransactions ( ) ; int retryCount = retryCountOnLockTimeout ; try { Transactions . Transaction txn = txns . begin ( ) ; if ( keysToLock . length > 0 ) { List < String > keysList = Arrays . asList ( keysToLock ) ; boolean locksAcquired = false ; while ( ! locksAcquired && retryCountOnLockTimeout -- >= 0 ) { locksAcquired = lockDocuments ( keysList ) ; } if ( ! locksAcquired ) { txn . rollback ( ) ; throw new org . modeshape . jcr . TimeoutException ( "Cannot acquire locks on: " + Arrays . toString ( keysToLock ) + " after " + retryCount + " attempts" ) ; } } try { V result = operation . call ( ) ; txn . commit ( ) ; return result ; } catch ( Exception e ) { // always rollback txn . rollback ( ) ; // throw as is (see below) throw e ; } } catch ( IllegalStateException | SystemException | NotSupportedException err ) { throw new SystemFailureException ( err ) ; } catch ( RuntimeException re ) { throw re ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } }
Runs the given operation within a transaction after optionally locking some keys .