[Fix] Throw Exception on programmer mistake if no commitOnClose with +

This commit is contained in:
Robert von Burg 2017-10-05 09:54:42 +02:00
parent c69d5ec9a7
commit 4087608e1a
3 changed files with 28 additions and 28 deletions

View File

@ -918,7 +918,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
exc); //$NON-NLS-1$ exc); //$NON-NLS-1$
} }
logger.error("Transaction failed due to " + e.getMessage(), e); logger.error("Transaction failed due to " + e.getMessage(), e);
handleFailure(start, ex); handleFailure(false, start, ex);
} }
try { try {
@ -927,14 +927,12 @@ public abstract class AbstractTransaction implements StrolchTransaction {
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Transaction failed due to " + e.getMessage(), e); logger.error("Transaction failed due to " + e.getMessage(), e);
logger.error("Failed to commit transaction and then rollback due to " + ex.getMessage(), ex); logger.error("Failed to commit transaction and then rollback due to " + ex.getMessage(), ex);
handleFailure(start, e); handleFailure(false, start, e);
} }
this.txResult.setState(TransactionState.FAILED); this.txResult.setState(TransactionState.FAILED);
String msg = "Strolch Transaction for realm {0} failed due to {1}"; //$NON-NLS-1$ handleFailure(true, start, e);
msg = MessageFormat.format(msg, getRealmName(), ExceptionHelper.getExceptionMessage(e));
throw new StrolchTransactionException(msg, e);
} finally { } finally {
releaseElementLocks(); releaseElementLocks();
@ -952,7 +950,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
handleRollback(start); handleRollback(start);
this.txResult.setState(TransactionState.ROLLED_BACK); this.txResult.setState(TransactionState.ROLLED_BACK);
} catch (Exception e) { } catch (Exception e) {
handleFailure(start, e); handleFailure(true, start, e);
this.txResult.setState(TransactionState.FAILED); this.txResult.setState(TransactionState.FAILED);
} finally { } finally {
releaseElementLocks(); releaseElementLocks();
@ -962,6 +960,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
@Override @Override
public void autoCloseableReadOnly() throws StrolchTransactionException { public void autoCloseableReadOnly() throws StrolchTransactionException {
long start = System.nanoTime(); long start = System.nanoTime();
boolean throwException = false;
try { try {
this.txResult.setState(TransactionState.CLOSING); this.txResult.setState(TransactionState.CLOSING);
@ -995,7 +994,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
} catch (Exception e1) { } catch (Exception e1) {
logger.error("Failed to rollback after exception", e); logger.error("Failed to rollback after exception", e);
} }
handleFailure(start, e); handleFailure(true, start, e);
this.txResult.setState(TransactionState.FAILED); this.txResult.setState(TransactionState.FAILED);
} finally { } finally {
releaseElementLocks(); releaseElementLocks();
@ -1114,7 +1113,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
logger.error(sb.toString()); logger.error(sb.toString());
} }
private void handleFailure(long closeStartNanos, Exception e) { private void handleFailure(boolean throwEx, long closeStartNanos, Exception e) {
long end = System.nanoTime(); long end = System.nanoTime();
long txDuration = end - this.txResult.getStartNanos(); long txDuration = end - this.txResult.getStartNanos();
@ -1153,7 +1152,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
msg = MessageFormat.format(msg, getRealmName(), ExceptionHelper.getExceptionMessage(e), sb.toString()); msg = MessageFormat.format(msg, getRealmName(), ExceptionHelper.getExceptionMessage(e), sb.toString());
StrolchTransactionException ex = new StrolchTransactionException(msg, e); StrolchTransactionException ex = new StrolchTransactionException(msg, e);
if (this.closeStrategy == TransactionCloseStrategy.COMMIT) if (throwEx)
throw ex; throw ex;
else else
logger.error("Transaction failed in non-commit mode! Logging TX exception, so exception is not suppressed.", logger.error("Transaction failed in non-commit mode! Logging TX exception, so exception is not suppressed.",

View File

@ -18,6 +18,7 @@ package li.strolch.service;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import li.strolch.agent.api.StrolchAgent;
import li.strolch.model.ModelGenerator; import li.strolch.model.ModelGenerator;
import li.strolch.model.Order; import li.strolch.model.Order;
import li.strolch.model.Resource; import li.strolch.model.Resource;
@ -190,7 +191,7 @@ public class TxTest extends AbstractRealmServiceTest {
@Override @Override
protected ServiceResult internalDoService(ServiceArgument arg) throws Exception { protected ServiceResult internalDoService(ServiceArgument arg) throws Exception {
String id = "flushSuccessfully"; String id = StrolchAgent.getUniqueId();
Resource resource = ModelGenerator.createResource(id, id, id); Resource resource = ModelGenerator.createResource(id, id, id);
boolean txFailed = false; boolean txFailed = false;
@ -203,7 +204,7 @@ public class TxTest extends AbstractRealmServiceTest {
txFailed = true; txFailed = true;
} }
assertTrue("TX should have failed!", txFailed); assertTrue("TX should have failed as commitOnClose is missing!", txFailed);
return ServiceResult.success(); return ServiceResult.success();
} }

View File

@ -33,7 +33,6 @@ import li.strolch.xmlpers.objref.TypeRef;
/** /**
* @author Robert von Burg <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
*
*/ */
public class ObjectDao { public class ObjectDao {
@ -54,7 +53,7 @@ public class ObjectDao {
assertNotNull(object); assertNotNull(object);
PersistenceContext<T> ctx = createCtx(object); PersistenceContext<T> ctx = createCtx(object);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.add(ctx.getObjectRef().getType(), ctx); this.objectFilter.add(ctx.getObjectRef().getType(), ctx.getObjectRef(), ctx);
} }
public <T> void addAll(List<T> objects) { public <T> void addAll(List<T> objects) {
@ -64,7 +63,7 @@ public class ObjectDao {
for (T object : objects) { for (T object : objects) {
PersistenceContext<T> ctx = createCtx(object); PersistenceContext<T> ctx = createCtx(object);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.add(ctx.getObjectRef().getType(), ctx); this.objectFilter.add(ctx.getObjectRef().getType(), ctx.getObjectRef(), ctx);
} }
} }
} }
@ -74,7 +73,7 @@ public class ObjectDao {
assertNotNull(object); assertNotNull(object);
PersistenceContext<T> ctx = createCtx(object); PersistenceContext<T> ctx = createCtx(object);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.update(ctx.getObjectRef().getType(), ctx); this.objectFilter.update(ctx.getObjectRef().getType(), ctx.getObjectRef(), ctx);
} }
public <T> void updateAll(List<T> objects) { public <T> void updateAll(List<T> objects) {
@ -84,7 +83,7 @@ public class ObjectDao {
for (T object : objects) { for (T object : objects) {
PersistenceContext<T> ctx = createCtx(object); PersistenceContext<T> ctx = createCtx(object);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.update(ctx.getObjectRef().getType(), ctx); this.objectFilter.update(ctx.getObjectRef().getType(), ctx.getObjectRef(), ctx);
} }
} }
} }
@ -94,7 +93,7 @@ public class ObjectDao {
assertNotNull(object); assertNotNull(object);
PersistenceContext<T> ctx = createCtx(object); PersistenceContext<T> ctx = createCtx(object);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.remove(ctx.getObjectRef().getType(), ctx); this.objectFilter.remove(ctx.getObjectRef().getType(), ctx.getObjectRef(), ctx);
} }
public <T> void removeAll(List<T> objects) { public <T> void removeAll(List<T> objects) {
@ -104,7 +103,7 @@ public class ObjectDao {
for (T object : objects) { for (T object : objects) {
PersistenceContext<T> ctx = createCtx(object); PersistenceContext<T> ctx = createCtx(object);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.remove(ctx.getObjectRef().getType(), ctx); this.objectFilter.remove(ctx.getObjectRef().getType(), ctx.getObjectRef(), ctx);
} }
} }
} }
@ -132,7 +131,7 @@ public class ObjectDao {
PersistenceContext<T> ctx = createCtx(idRef); PersistenceContext<T> ctx = createCtx(idRef);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.remove(ctx.getObjectRef().getType(), ctx); this.objectFilter.remove(ctx.getObjectRef().getType(), ctx.getObjectRef(), ctx);
removed++; removed++;
} }
} }
@ -161,7 +160,7 @@ public class ObjectDao {
PersistenceContext<T> ctx = createCtx(idRef); PersistenceContext<T> ctx = createCtx(idRef);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.remove(ctx.getObjectRef().getType(), ctx); this.objectFilter.remove(ctx.getObjectRef().getType(), ctx.getObjectRef(), ctx);
removed++; removed++;
} }
} finally { } finally {
@ -176,7 +175,7 @@ public class ObjectDao {
assertIsIdRef(objectRef); assertIsIdRef(objectRef);
PersistenceContext<T> ctx = createCtx(objectRef); PersistenceContext<T> ctx = createCtx(objectRef);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.remove(objectRef.getType(), ctx); this.objectFilter.remove(objectRef.getType(), ctx.getObjectRef(), ctx);
} }
public <T> void removeAll(ObjectRef parentRef) { public <T> void removeAll(ObjectRef parentRef) {
@ -193,7 +192,7 @@ public class ObjectDao {
ObjectRef childRef = parentRef.getChildIdRef(this.tx, id); ObjectRef childRef = parentRef.getChildIdRef(this.tx, id);
PersistenceContext<T> ctx = createCtx(childRef); PersistenceContext<T> ctx = createCtx(childRef);
ctx.getObjectRef().lock(); ctx.getObjectRef().lock();
this.objectFilter.remove(childRef.getType(), ctx); this.objectFilter.remove(childRef.getType(), ctx.getObjectRef(), ctx);
} }
} finally { } finally {
parentRef.unlock(); parentRef.unlock();
@ -206,7 +205,7 @@ public class ObjectDao {
objectRef.lock(); objectRef.lock();
try { try {
PersistenceContext<T> ctx = objectRef.<T> createPersistenceContext(this.tx); PersistenceContext<T> ctx = objectRef.<T>createPersistenceContext(this.tx);
return this.fileDao.exists(ctx); return this.fileDao.exists(ctx);
} finally { } finally {
objectRef.unlock(); objectRef.unlock();
@ -219,7 +218,7 @@ public class ObjectDao {
objectRef.lock(); objectRef.lock();
try { try {
PersistenceContext<T> ctx = objectRef.<T> createPersistenceContext(this.tx); PersistenceContext<T> ctx = objectRef.<T>createPersistenceContext(this.tx);
this.fileDao.performRead(ctx); this.fileDao.performRead(ctx);
return ctx.getObject(); return ctx.getObject();
} finally { } finally {
@ -288,18 +287,19 @@ public class ObjectDao {
} }
public <T> PersistenceContext<T> createCtx(T object) { public <T> PersistenceContext<T> createCtx(T object) {
return this.ctxFactoryDelegator.<T> getCtxFactory(object.getClass()).createCtx(this.tx.getObjectRefCache(), return this.ctxFactoryDelegator.<T>getCtxFactory(object.getClass())
object); .createCtx(this.tx.getObjectRefCache(), object);
} }
public <T> PersistenceContext<T> createCtx(ObjectRef objectRef) { public <T> PersistenceContext<T> createCtx(ObjectRef objectRef) {
String type = objectRef.getType(); String type = objectRef.getType();
PersistenceContextFactory<T> ctxFactory = this.ctxFactoryDelegator.<T> getCtxFactory(type); PersistenceContextFactory<T> ctxFactory = this.ctxFactoryDelegator.<T>getCtxFactory(type);
return ctxFactory.createCtx(objectRef); return ctxFactory.createCtx(objectRef);
} }
private void assertNotClosed() { private void assertNotClosed() {
if (!this.tx.isOpen()) if (!this.tx.isOpen())
throw new IllegalStateException("Transaction has been closed and thus no operation can be performed!"); //$NON-NLS-1$ throw new IllegalStateException(
"Transaction has been closed and thus no operation can be performed!"); //$NON-NLS-1$
} }
} }