我在下面粘贴了我的代码。在我们的应用程序中,他们将事务设置在线程本地。
实际上我的疑问是我们为什么需要这个?
如果我们没有在 threadlocal 中设置事务会发生什么?
public void beginTransaction() {
final String METHOD_NAME = "beginTransaction";
log.entering(CLASS_NAME, METHOD_NAME);
PcUtilLogging.logTransactionLifecycle("Begin Transaction",
this.persistenceConfigurationKey);
// Initialize.
final PcRequestContext context = PcRequestContext.getInstance();
final PersistenceManager pm =
context.getPersistenceManager(this.persistenceConfigurationKey);
try {
// Begin a new transaction.
final Transaction transaction = pm.newTransaction();
// Set the Transaction in ThreadLocal.
context.setTransaction(this.persistenceConfigurationKey,
transaction);
} catch (final Exception e) {
// Throw.
throw new PcTransactionException(
new ApplicationExceptionAttributes.Builder(CLASS_NAME, METHOD_NAME).build(),
"Error encountered while attempting to begin a ["
+ this.getPersistenceConfigurationKey()
+ "] transaction.", e);
}
log.exiting(CLASS_NAME, METHOD_NAME);
return;
}