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

Make ConnectionConfig#autoCommit configurable via fake pragma #1027

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

exoego
Copy link

@exoego exoego commented Nov 28, 2023

Problem

I am using Kotlin's SQL Framework https://github.com/JetBrains/Exposed with sqlite-jdbc.

import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.transactions.transaction
(snip)

val sqLiteConfig = SQLiteConfig()
val ds = SQLiteDataSource(sqLiteConfig)
val database = Database.connect(ds)

import org.jetbrains.exposed.sql.transactions.transaction
import org.sqlite.SQLiteErrorCode
import org.sqlite.SQLiteException
import java.time.temporal.Temporal

transaction(db) {
  connection.autoCommit = false
  // do something with DB
}

I want to disable auto-commit for better performance, but it appears that it is not configurable via SQLiteConfig nor SQLiteDataSource.
SQLiteConnectionConfig offers setAutoCommit, but SQLiteConfig#defaultConnectionConfig always use autoCommit = true.

private boolean autoCommit = true;

One can specify connection.autoCommit = false in transaction { ... } DSL, but it is error-prone because one might forget.

Proposal

This PR adds "fake" pragma jdbc.auto_commit, so auto-commit can be configurable via SQLiteConfig.

val prop = new Properties()
prop.setProperty(SQLiteConfig.Pragma.JDBC_AUTO_COMMIT.pragmaName, "true")
val sqLiteConfig = SQLiteConfig(prop)
val ds = SQLiteDataSource(sqLiteConfig)
val database = Database.connect(ds)

@gotson
Copy link
Collaborator

gotson commented Nov 28, 2023

It seems JDBC expects the default to always be true, according to https://stackoverflow.com/a/11022406

It would probably make sense for the exposed framework to allow for such option so that new connections are always set to false.

@exoego
Copy link
Author

exoego commented Nov 28, 2023

I agree autoCommit = true should be the default.
This PR does not change that.
It just allows users to configure autoCommit so they can use this driver for any framework in autoCommit = false mode.

Several JDBC libs offer a configuration point of autoCommit:

@exoego
Copy link
Author

exoego commented Dec 21, 2023

Please take a look.

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 this pull request may close these issues.

2 participants