From the docs (https://cayenne.apache.org/docs/4.2/cayenne-guide/#transactions):
If you are using Spring, EJB or another environment that manages transactions, you’ll likely need to switch the Cayenne runtime into "external transactions mode". This is done by setting the DI configuration property defined in Constants.SERVER_EXTERNAL_TX_PROPERTY (see Appendix A). If this property is set to "true", Cayenne assumes that JDBC Connections obtained by runtime, whenever that might happen, are all coming from a transactional DataSource managed by the container. In this case, Cayenne does not attempt to commit or roll back the connections, leaving it up to the container to do that when appropriate. In your description, you don’t hint at using some other environment/container (e.g., Spring, EJB) which would manage the transactions, so it seems that, as John Huss suggested, you should not enable external transactions. > On Nov 11, 2024, at 3:58 PM, Christian Gonzalez > <christian.gonza...@smartscrubs.com> wrote: > > Hi John, I tried making the transaction using the transaction factory and > the changes that the objectContext committed were still reflected in the > database. I added a print statement before the transaction is supposed to > be rolled back and noticed that when it does the cayenne logs show these > messages. > > INFO o.apache.cayenne.log.JdbcEventLogger - *** no rollback - transaction > controlled externally. > INFO o.apache.cayenne.log.JdbcEventLogger - *** no rollback - transaction > controlled externally. > > Does this mean the tx.rollback() was not performed? > > On Fri, Nov 8, 2024 at 5:38 PM John Huss <johnth...@gmail.com> wrote: > >> 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 >>>> <https://cayenne.apache.org/docs/4.2/cayenne-guide/>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? >>>> >>> >>