-
-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ClassCastException while using DB.beginTransaction in BeanPersistAdapter #3320
Comments
Well no it isn't. BeanPersistAdapter are occurring DURING the execution of the transaction - so it's not ok to commit() that transaction which is effectively what is happening here. We can access the current transaction via @Singleton
public class AdSyncQueueAdapter extends BeanPersistAdapter {
[...]
@Override
public void postUpdate(BeanPersistRequest<?> request) {
// get the current transaction
Transaction transaction = request.transaction();
// get the current database
Database database = request.database();
Log newLog= new Log();
newLog.setText("Test");
database.save(newLog, transaction);
} If the code used DB.createTransaction() instead of DB.beginTransaction() and created a NEW transaction [that isn't put into the thread local etc] then that might be closer to the intention BUT ... I'll suggest this isn't a good idea in that this postUpdate() is being executed during a transaction and its generally the case that whatever is done here is done using that transaction and will succeed or fail with that current transaction. That is, very unusual to create a new transaction in a BeanPersistAdapter. The javadoc for BeanPersistAdapter is poor and doesn't say this. Note that BeanPersistListener are non-transactional in that they are executed outside of a transaction after the successful commit of a transaction. |
We can close this issue right? |
Hi @rbygrave, |
We searched in the Ebean documentation of
BeanPersistAdapter
, issues, discussions, and google forum but cannot find out if the usage ofDB.beginTransaction
is authorized inBeanPersistAdapter
. Sorry, if we missed the information somewhere.We tried to write some code in Adapter that insert/update DB rows during
postUpdate
and we usedDB.beginTransaction
the same way we would have done in main workflow.However, we notice that this is working only when the initial
DB.save
have been encapsulated in aDB.beginTransaction
block.If we don't and call immediately
DB.save
, it create/use aJdbcTransaction
that end up in aClassCasException
.Additional question :
Considering
DB.save
is wrapped usingDB.beginTransaction
. Is callingDB.beginTransaction
in the Adapter will ensure us that the rows will be inserted/updated in the same transaction or is it better to get transaction throughrequest.transaction();
and passing it while callingDB.update(bean, transaction)
?Expected behavior
Using
DB.beginTransaction
in aBeanPersistAdapter
may reuse the current transaction used when callingDB.save
.Actual behavior
A
ClassCastException
occurs when we callDB.beginTransaction
in an Adapter and no transaction opened aroundDB.save
.Steps to reproduce
Thanks in advance for your help and tips.
Yours faithfully,
LCDP
The text was updated successfully, but these errors were encountered: