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) {
RequestEvent.Type type = event.getType();
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();
}
}

View File

@ -34,16 +34,18 @@ public class JdbiTransactionManager {
public void begin(boolean autoCommit) {
try {
Handle handle = handleManager.get();
handle.getConnection().setAutoCommit(autoCommit);
handle.getConfig(Handles.class).setForceEndTransactions(false);
handle.begin();
IN_TRANSACTION_HANDLES.add(handle.hashCode());
LOG.debug(
"Begin Transaction Thread Id [{}] has handle id [{}] Transaction {} Level {}",
Thread.currentThread().getId(),
handle.hashCode(),
handle.isInTransaction(),
handle.getTransactionIsolationLevel());
if (autoCommit) {
handle.getConnection().setAutoCommit(autoCommit);
handle.getConfig(Handles.class).setForceEndTransactions(false);
handle.begin();
IN_TRANSACTION_HANDLES.add(handle.hashCode());
LOG.debug(
"Begin Transaction Thread Id [{}] has handle id [{}] Transaction {} Level {}",
Thread.currentThread().getId(),
handle.hashCode(),
handle.isInTransaction(),
handle.getTransactionIsolationLevel());
}
} catch (Exception ex) {
terminateHandle();
}
@ -71,7 +73,7 @@ public class JdbiTransactionManager {
}
public void rollback() {
if (IN_TRANSACTION_HANDLES.contains(handleManager.get().hashCode())) {
if (handleManager.handleExists()) {
Handle handle = handleManager.get();
if (handle == null) {
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());
} else {
Object dao;
Object result;
if (JdbiUnitOfWorkProvider.getInstance().getHandleManager().handleExists()) {
Handle handle = JdbiUnitOfWorkProvider.getInstance().getHandle();
LOG.debug(
@ -53,16 +54,24 @@ public class ManagedHandleInvocationHandler<T> implements InvocationHandler {
handle.hashCode());
dao = handle.attach(underlying);
result = invokeMethod(method, dao, args);
} else {
// 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 {
return method.invoke(dao, args);
} catch (Exception ex) {
throw ex.getCause();
}
private Object invokeMethod(Method method, Object dao, Object[] args) throws Throwable {
try {
return method.invoke(dao, args);
} catch (Exception ex) {
throw ex.getCause();
}
}