Skip to content
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

Transaction fails if no queries executed #87

Open
ShansOwn opened this issue Dec 12, 2020 · 0 comments · May be fixed by #88
Open

Transaction fails if no queries executed #87

ShansOwn opened this issue Dec 12, 2020 · 0 comments · May be fixed by #88

Comments

@ShansOwn
Copy link

ShansOwn commented Dec 12, 2020

I have multiple queries that run depending on some conditions inside a transaction. If all the conditions are false no query is run and transaction fails with the exception:

org.codejargon.fluentjdbc.api.FluentJdbcSqlException: Error executing transaction
	at org.codejargon.fluentjdbc.internal.query.TransactionInternal.lambda$inNewTransaction$1(TransactionInternal.java:68)
	at org.codejargon.fluentjdbc.api.integration.providers.DataSourceConnectionProvider.provide(DataSourceConnectionProvider.java:23)
	at org.codejargon.fluentjdbc.internal.query.TransactionInternal.inNewTransaction(TransactionInternal.java:51)
	at org.codejargon.fluentjdbc.internal.query.TransactionInternal.in(TransactionInternal.java:36)
	at org.codejargon.fluentjdbc.internal.query.TransactionInternal.inNoResult(TransactionInternal.java:42)
...
Caused by: org.postgresql.util.PSQLException: Cannot commit when autoCommit is enabled.
	at org.postgresql.jdbc.PgConnection.commit(PgConnection.java:854)
	at com.zaxxer.hikari.pool.ProxyConnection.commit(ProxyConnection.java:366)
	at com.zaxxer.hikari.pool.HikariProxyConnection.commit(HikariProxyConnection.java)
	at org.codejargon.fluentjdbc.internal.query.TransactionInternal.lambda$inNewTransaction$1(TransactionInternal.java:66)

Pseudocode:

query.transaction().inNoResult(() ->
  if (condition1) {
    var condition3 = query.update();
  }
  if (condition2 || condition3) {
    query.update()
  }
  if (condition3) {
    query.update()
  }
)

This happens because the connection is committed while having autoCommit = true as the transaction wasn't actually started.

I suggest avoiding connection commit if it has autoCommit = true. Please see my PR.

@ShansOwn ShansOwn linked a pull request Dec 12, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant