[New] Added TransactionThreadLocal
This commit is contained in:
parent
2953a40aef
commit
5c5487efdc
|
@ -103,8 +103,6 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
private Certificate certificate;
|
private Certificate certificate;
|
||||||
private PrivilegeContext privilegeContext;
|
private PrivilegeContext privilegeContext;
|
||||||
|
|
||||||
private static final ThreadLocal<StrolchTransaction> threadLocalTx = new ThreadLocal<>();
|
|
||||||
|
|
||||||
public AbstractTransaction(ComponentContainer container, StrolchRealm realm, Certificate certificate, String action,
|
public AbstractTransaction(ComponentContainer container, StrolchRealm realm, Certificate certificate, String action,
|
||||||
boolean readOnly) {
|
boolean readOnly) {
|
||||||
DBC.PRE.assertNotNull("container must be set!", container); //$NON-NLS-1$
|
DBC.PRE.assertNotNull("container must be set!", container); //$NON-NLS-1$
|
||||||
|
@ -112,10 +110,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
DBC.PRE.assertNotNull("certificate must be set!", certificate); //$NON-NLS-1$
|
DBC.PRE.assertNotNull("certificate must be set!", certificate); //$NON-NLS-1$
|
||||||
DBC.PRE.assertNotNull("action must be set!", action); //$NON-NLS-1$
|
DBC.PRE.assertNotNull("action must be set!", action); //$NON-NLS-1$
|
||||||
|
|
||||||
if (threadLocalTx.get() != null)
|
TransactionThreadLocal.setTx(this);
|
||||||
logger.error("THIS THREAD HAS ALREADY OPENED A TX!");
|
|
||||||
else
|
|
||||||
threadLocalTx.set(this);
|
|
||||||
|
|
||||||
this.container = container;
|
this.container = container;
|
||||||
this.privilegeHandler = container.getPrivilegeHandler();
|
this.privilegeHandler = container.getPrivilegeHandler();
|
||||||
|
@ -1530,7 +1525,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
releaseElementLocks();
|
releaseElementLocks();
|
||||||
threadLocalTx.remove();
|
TransactionThreadLocal.removeTx();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1549,7 +1544,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
this.txResult.setState(TransactionState.FAILED);
|
this.txResult.setState(TransactionState.FAILED);
|
||||||
} finally {
|
} finally {
|
||||||
releaseElementLocks();
|
releaseElementLocks();
|
||||||
threadLocalTx.remove();
|
TransactionThreadLocal.removeTx();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1593,7 +1588,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
this.txResult.setState(TransactionState.FAILED);
|
this.txResult.setState(TransactionState.FAILED);
|
||||||
} finally {
|
} finally {
|
||||||
releaseElementLocks();
|
releaseElementLocks();
|
||||||
threadLocalTx.remove();
|
TransactionThreadLocal.removeTx();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package li.strolch.persistence.api;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class TransactionThreadLocal extends ThreadLocal<StrolchTransaction> {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(TransactionThreadLocal.class);
|
||||||
|
|
||||||
|
private static final TransactionThreadLocal instance = new TransactionThreadLocal();
|
||||||
|
|
||||||
|
public static StrolchTransaction getTx() {
|
||||||
|
StrolchTransaction tx = instance.get();
|
||||||
|
if (tx == null)
|
||||||
|
throw new IllegalStateException("No TX available on thread " + Thread.currentThread().getName());
|
||||||
|
return tx;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setTx(StrolchTransaction tx) {
|
||||||
|
if (instance.get() != null)
|
||||||
|
logger.error("THIS THREAD HAS ALREADY OPENED A TX!");
|
||||||
|
else
|
||||||
|
instance.set(tx);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeTx() {
|
||||||
|
instance.remove();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue