Transactions

2024-11-08 Thread Christian Gonzalez
Hello, I am currently using cayenne 4.2 and am running into some issues
when committing my changes. We have an application that uses a single
object context to do all the necessary changes we want to save to the
database and then when the user clicks the save button we call the
objectContext.commit(). The issue is that if a commit exception happens
during this process we end up with half committed data as the transaction
doesn't get rolled back. From what I understand, if I were to capture
the exception and do objectyContext.rollbackChanges it would only remove
the changes to the object context, not actually rollback the changes in
the database. I also tried a mixture of this example provided for
transactions in the cayenne documentation
and this example at the
bottom from apache
.
Essentially I'm calling the runtime.performInTransaction and inside of the
TransactionOperation using the BaseTransaction.getThreadTransaction()
method to get the transaction, then calling the objectContext.commit and
after calling that method doing transaction.commit(). Iif an exception
happens I call transaction.rollback() but once it's all done I still see
the changes that were sent before the exception present in the database and
when I look at the logs of the SQL that gets sent I don't see a transaction
started. My question is am I using the transaction correctly or how do I
get all the changes in the object context to be reversed if an exception
happens?


Re: Transactions

2024-11-08 Thread Christian Gonzalez
Also forgot to mention but the runtime is configured with external
transactions enabled.

On Fri, Nov 8, 2024 at 4:14 PM Christian Gonzalez <
christian.gonza...@smartscrubs.com> wrote:

> Hello, I am currently using cayenne 4.2 and am running into some issues
> when committing my changes. We have an application that uses a single
> object context to do all the necessary changes we want to save to the
> database and then when the user clicks the save button we call the
> objectContext.commit(). The issue is that if a commit exception happens
> during this process we end up with half committed data as the transaction
> doesn't get rolled back. From what I understand, if I were to capture
> the exception and do objectyContext.rollbackChanges it would only remove
> the changes to the object context, not actually rollback the changes in
> the database. I also tried a mixture of this example provided for
> transactions in the cayenne documentation
> and this example at
> the bottom from apache
> .
> Essentially I'm calling the runtime.performInTransaction and inside of the
> TransactionOperation using the BaseTransaction.getThreadTransaction()
> method to get the transaction, then calling the objectContext.commit and
> after calling that method doing transaction.commit(). Iif an exception
> happens I call transaction.rollback() but once it's all done I still see
> the changes that were sent before the exception present in the database and
> when I look at the logs of the SQL that gets sent I don't see a transaction
> started. My question is am I using the transaction correctly or how do I
> get all the changes in the object context to be reversed if an exception
> happens?
>


Re: Transactions

2024-11-08 Thread John Huss
I do manual transaction handling like this:

TransactionFactory txFactory =
CayenneRuntime.getThreadInjector().getInstance(TransactionFactory.class);
Transaction tx = txFactory.createTransaction();
tx.begin();
try {
 // do work

 context.commitChanges();
 tx.commit();

} catch (Exception e) {
 tx.rollback();
 throw e;
}

On Fri, Nov 8, 2024 at 5:21 PM Christian Gonzalez <
christian.gonza...@smartscrubs.com> wrote:

> Also forgot to mention but the runtime is configured with external
> transactions enabled.
>
> On Fri, Nov 8, 2024 at 4:14 PM Christian Gonzalez <
> christian.gonza...@smartscrubs.com> wrote:
>
> > Hello, I am currently using cayenne 4.2 and am running into some issues
> > when committing my changes. We have an application that uses a single
> > object context to do all the necessary changes we want to save to the
> > database and then when the user clicks the save button we call the
> > objectContext.commit(). The issue is that if a commit exception happens
> > during this process we end up with half committed data as the transaction
> > doesn't get rolled back. From what I understand, if I were to capture
> > the exception and do objectyContext.rollbackChanges it would only remove
> > the changes to the object context, not actually rollback the changes in
> > the database. I also tried a mixture of this example provided for
> > transactions in the cayenne documentation
> > and this example at
> > the bottom from apache
> > <
> https://nightlies.apache.org/cayenne/docbook/index/persistent-objects-objectcontext.html
> >.
> > Essentially I'm calling the runtime.performInTransaction and inside of
> the
> > TransactionOperation using the BaseTransaction.getThreadTransaction()
> > method to get the transaction, then calling the objectContext.commit and
> > after calling that method doing transaction.commit(). Iif an exception
> > happens I call transaction.rollback() but once it's all done I still see
> > the changes that were sent before the exception present in the database
> and
> > when I look at the logs of the SQL that gets sent I don't see a
> transaction
> > started. My question is am I using the transaction correctly or how do I
> > get all the changes in the object context to be reversed if an exception
> > happens?
> >
>