0
我在下面粘貼了我的代碼。在我們的應用程序中,他們在線程本地設置事務。
其實我的疑問是爲什麼我們需要這個?
如果我們沒有在threadlocal中設置tranaction會發生什麼?我們是否真的需要在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;
}
thanks @augusto –