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

Postgres has auto commit on by default making commit / rollback not an option #21

Open
janvladimirmostert opened this issue Feb 4, 2019 · 1 comment

Comments

@janvladimirmostert
Copy link

janvladimirmostert commented Feb 4, 2019

val dbConnection = PgConnection.create("jdbc:postgresql://localhost:5432/mydb")
val transaction = dbConnection.createTransaction()
update(TestEntity).set(
    TestEntity.name, "BLEH ${Random().nextInt()}"
).where(TestEntity.id eq 1).executeOn(transaction)
transaction.commit()

This causes a postgres error:

Cannot commit when autoCommit is enabled.

C3P0, Hikari and regular JDBC allows you to turn off autocommit on the connection itself.

Where would be a reasonable place to add such a property, on PgConnection and its parent classes themselves?

val dbConnection = PgConnection.create(
    "jdbc:postgresql://localhost:5432/mydb", 
    ConnectionOptions(autoCommit = false
))

The workaround for now is to switch off autocommit on the DB level which is not always an option when using a DB hosted by someone else.

@codesplode
Copy link
Contributor

Hi Jan,

Yes, you are correct on where to add the options. I for example use a connection pool interface that I pass through to that parses the jdbc url from json as a url property and map of options. Depending on your application, and your want for access to more options / more flexibility in creating connections, you can wrap your own connections with a squash Transaction / statement using the code you post. This is a simple example from one of my projects:

	override fun createTransaction() = createConnection().createTransaction()

	private fun createConnection():JDBCConnection = MySqlConnection {
		datasource.connection
	}

In this example, datasource.connection is a computed property that gives a connection from our connection pool each time. The createConnection() method is wrapping the pool connection in a squash MySqlConnection (squash JDBCConnection), and the JDBCConnection interface has a method to createTransaction() from it.

This allows you full control of your connections and hopefully serves to both solve your problem, and potentially help you help the project by adding options to Postgres.

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

No branches or pull requests

2 participants