How to create entity for additional DataStore inside ServiceBean?

Hi all,

I am storing in an additional datastore a new entity. The process runs smoothly but according to the documentation:

All listeners except BeforeAttachEntityListener work within a transaction. It means that if an exception is thrown inside the listener, the current transaction is rolled back and all database changes are discarded.

but on the other hand the method signatures in the listeners do not throw Exceptions and if attempting there arises a name clash. So how should I manage it in case there arises an Exception?

I tried to look in the Transaction object to see if I could mark it to rollback, but thereś not such method, but others with confusing names, like commitRetaining…

My method implementation is:

@Override
public void onBeforeInsert(ArchivoAdjunto entity, EntityManager entityManager){
int y = 2;
try {
AppBeans.get(ColeccionArchivosAdjuntosService.class).generaNuevoArchivoAdjuntoExt(entity);
} catch (Exception e) {
//how can I rollback both transactions from here???
}
}

Any ideas?

Thanks again for your help.

Regards,
Carlos.

Hi,

Just use RuntimeException or create your own sub-class of RuntimeException.

It is a basic Java. Read more about checked and unchecked exceptions, e.g. Checked and unchecked exceptions in java with examples

Hi Alex,
Thanks again. Runtime exception seems perfect.
Carlos.