Search code examples
jdbcautocommit

Acquiring Autocommit Disabled DB Connection


I would like to know if there is a way to say pass autocommit = false in "info" parameter to DriverManager.getConnection(String url, Properties info) and get db connection which shall have autocommit disabled by default. I know that I can get the connection and then invoke setAutocommit to false but would like to know if this is achievable.


Solution

  • In general: no. The JDBC specification specifies that newly created connections are initially autoCommit = true. From JDBC 4.2, section 10.1.1:

    The default is for auto-commit mode to be enabled when the Connection object is created.

    However the use of default might mean that drivers are allowed to have an alternative config. Unfortunately the language in the JDBC spec isn't always formal, so the intention might be that a Connection must always be auto-commit initially.

    So it could be that there are drivers that have a property to disable autoCommit initially, but that is something that you should look up in the documentation of that specific driver. That would only apply to that specific driver, and not for all JDBC drivers.