@@ -39,7 +39,7 @@ public class KieSpringTransactionManager
3939 private AbstractPlatformTransactionManager ptm ;
4040
4141 TransactionDefinition td = new DefaultTransactionDefinition ();
42- TransactionStatus currentTransaction = null ;
42+ ThreadLocal < TransactionStatus > currentTransaction = new ThreadLocal <>() ;
4343
4444 public KieSpringTransactionManager (AbstractPlatformTransactionManager ptm ) {
4545 this .ptm = ptm ;
@@ -50,14 +50,14 @@ public boolean begin() {
5050 // RHBPMS-4621 - transaction can be marked as rollback
5151 // and still be associated with current thread
5252 // See WFLY-4327
53- if ( getStatus () == TransactionManager .STATUS_ROLLEDBACK ) {
54- logger .debug ( "Cleanup of transaction that has been rolled back previously " );
53+ if (getStatus () == TransactionManager .STATUS_ROLLEDBACK ) {
54+ logger .warn ( "Cleaning up rolledback transaction " );
5555 rollback (true );
5656 }
5757 if (getStatus () == TransactionManager .STATUS_NO_TRANSACTION ) {
5858 // If there is no transaction then start one, we will commit within the same Command
5959 // it seems in spring calling getTransaction is enough to begin a new transaction
60- currentTransaction = this .ptm .getTransaction (td );
60+ currentTransaction . set ( this .ptm .getTransaction (td ) );
6161 return true ;
6262 } else {
6363 return false ;
@@ -81,37 +81,39 @@ public void commit(boolean transactionOwner) {
8181
8282 try {
8383 // if we didn't begin this transaction, then do nothing
84- this .ptm .commit (currentTransaction );
85- currentTransaction = null ;
86- if (TransactionSynchronizationManager .hasResource (KieSpringTransactionManager .RESOURCE_CONTAINER )) {
87- TransactionSynchronizationManager .unbindResource (KieSpringTransactionManager .RESOURCE_CONTAINER );
88- }
84+ this .ptm .commit (currentTransaction .get ());
8985 } catch (Exception e ) {
9086 logger .warn ("Unable to commit transaction" ,
9187 e );
9288 throw new RuntimeException ("Unable to commit transaction" ,
9389 e );
90+ } finally {
91+ cleanupTransaction ();
9492 }
9593
9694 }
9795
9896 public void rollback (boolean transactionOwner ) {
99- try {
100- if (transactionOwner ) {
101- this .ptm .rollback (currentTransaction );
102- currentTransaction = null ;
103- if (TransactionSynchronizationManager .hasResource (KieSpringTransactionManager .RESOURCE_CONTAINER )) {
104- TransactionSynchronizationManager .unbindResource (KieSpringTransactionManager .RESOURCE_CONTAINER );
105- }
97+ if (transactionOwner ) {
98+ try {
99+ this .ptm .rollback (currentTransaction .get ());
100+ } catch (Exception e ) {
101+ logger .warn ("Unable to rollback transaction" , e );
102+ throw new RuntimeException ("Unable to rollback transaction" , e );
103+ } finally {
104+ cleanupTransaction ();
106105 }
107- } catch (Exception e ) {
108- logger .warn ("Unable to rollback transaction" ,
109- e );
110- throw new RuntimeException ("Unable to rollback transaction" ,
111- e );
112106 }
113107 }
114108
109+ private void cleanupTransaction () {
110+ if (TransactionSynchronizationManager .hasResource (KieSpringTransactionManager .RESOURCE_CONTAINER )) {
111+ TransactionSynchronizationManager .unbindResource (KieSpringTransactionManager .RESOURCE_CONTAINER );
112+ }
113+ TransactionSynchronizationManager .clear ();
114+ currentTransaction .remove ();
115+ }
116+
115117 /**
116118 * Borrowed from Seam
117119 */
@@ -124,13 +126,13 @@ public int getStatus() {
124126 if (TransactionSynchronizationManager .isActualTransactionActive ()) {
125127 TransactionStatus transaction = null ;
126128 try {
127- if (currentTransaction == null ) {
129+ if (currentTransaction . get () == null ) {
128130 transaction = ptm .getTransaction (td );
129131 if (transaction .isNewTransaction ()) {
130132 return TransactionManager .STATUS_COMMITTED ;
131133 }
132134 } else {
133- transaction = currentTransaction ;
135+ transaction = currentTransaction . get () ;
134136 }
135137 logger .debug ("Current TX: " + transaction );
136138 // If SynchronizationManager thinks it has an active transaction but
@@ -152,7 +154,7 @@ public int getStatus() {
152154 return TransactionManager .STATUS_ACTIVE ;
153155 }
154156 } finally {
155- if (currentTransaction == null && transaction != null ) {
157+ if (currentTransaction . get () == null && transaction != null ) {
156158 ptm .commit (transaction );
157159 }
158160 }
0 commit comments