set autocommit to true for non transactional (#13184)

* set autocommit to true for non transactional

* set autocommit to true for non transactional
This commit is contained in:
Sriharsha Chintalapani 2023-09-13 15:20:17 -07:00 committed by GitHub
parent 4f20070e6d
commit 03fc4d8be5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 18 deletions

View File

@ -13,7 +13,9 @@ class HttpGetRequestJdbiUnitOfWorkEventListener implements RequestEventListener
public void onEvent(RequestEvent event) { public void onEvent(RequestEvent event) {
RequestEvent.Type type = event.getType(); RequestEvent.Type type = event.getType();
LOG.debug("Handling GET Request Event {} {}", type, Thread.currentThread().getId()); LOG.debug("Handling GET Request Event {} {}", type, Thread.currentThread().getId());
if (type == RequestEvent.Type.FINISHED) { if (type == RequestEvent.Type.RESOURCE_METHOD_START) {
JdbiTransactionManager.getInstance().begin(true);
} else if (type == RequestEvent.Type.FINISHED) {
JdbiTransactionManager.getInstance().terminateHandle(); JdbiTransactionManager.getInstance().terminateHandle();
} }
} }

View File

@ -34,16 +34,18 @@ public class JdbiTransactionManager {
public void begin(boolean autoCommit) { public void begin(boolean autoCommit) {
try { try {
Handle handle = handleManager.get(); Handle handle = handleManager.get();
handle.getConnection().setAutoCommit(autoCommit); if (autoCommit) {
handle.getConfig(Handles.class).setForceEndTransactions(false); handle.getConnection().setAutoCommit(autoCommit);
handle.begin(); handle.getConfig(Handles.class).setForceEndTransactions(false);
IN_TRANSACTION_HANDLES.add(handle.hashCode()); handle.begin();
LOG.debug( IN_TRANSACTION_HANDLES.add(handle.hashCode());
"Begin Transaction Thread Id [{}] has handle id [{}] Transaction {} Level {}", LOG.debug(
Thread.currentThread().getId(), "Begin Transaction Thread Id [{}] has handle id [{}] Transaction {} Level {}",
handle.hashCode(), Thread.currentThread().getId(),
handle.isInTransaction(), handle.hashCode(),
handle.getTransactionIsolationLevel()); handle.isInTransaction(),
handle.getTransactionIsolationLevel());
}
} catch (Exception ex) { } catch (Exception ex) {
terminateHandle(); terminateHandle();
} }
@ -71,7 +73,7 @@ public class JdbiTransactionManager {
} }
public void rollback() { public void rollback() {
if (IN_TRANSACTION_HANDLES.contains(handleManager.get().hashCode())) { if (handleManager.handleExists()) {
Handle handle = handleManager.get(); Handle handle = handleManager.get();
if (handle == null) { if (handle == null) {
LOG.debug("Handle was found to be null during rollback for [{}]", Thread.currentThread().getId()); LOG.debug("Handle was found to be null during rollback for [{}]", Thread.currentThread().getId());

View File

@ -42,6 +42,7 @@ public class ManagedHandleInvocationHandler<T> implements InvocationHandler {
return getWrappedInstanceForDaoClass(method.getReturnType()); return getWrappedInstanceForDaoClass(method.getReturnType());
} else { } else {
Object dao; Object dao;
Object result;
if (JdbiUnitOfWorkProvider.getInstance().getHandleManager().handleExists()) { if (JdbiUnitOfWorkProvider.getInstance().getHandleManager().handleExists()) {
Handle handle = JdbiUnitOfWorkProvider.getInstance().getHandle(); Handle handle = JdbiUnitOfWorkProvider.getInstance().getHandle();
LOG.debug( LOG.debug(
@ -53,16 +54,24 @@ public class ManagedHandleInvocationHandler<T> implements InvocationHandler {
handle.hashCode()); handle.hashCode());
dao = handle.attach(underlying); dao = handle.attach(underlying);
result = invokeMethod(method, dao, args);
} else { } else {
// This is non-transactional request // This is non-transactional request
dao = JdbiUnitOfWorkProvider.getInstance().getHandleManager().getJdbi().onDemand(underlying); Handle handle = JdbiUnitOfWorkProvider.getInstance().getHandleManager().getJdbi().open();
handle.getConnection().setAutoCommit(true);
dao = handle.attach(underlying);
result = invokeMethod(method, dao, args);
handle.close();
} }
return result;
}
}
try { private Object invokeMethod(Method method, Object dao, Object[] args) throws Throwable {
return method.invoke(dao, args); try {
} catch (Exception ex) { return method.invoke(dao, args);
throw ex.getCause(); } catch (Exception ex) {
} throw ex.getCause();
} }
} }