Skip to content

Instantly share code, notes, and snippets.

@diogobaltazar
Last active December 30, 2017 03:07
Show Gist options
  • Save diogobaltazar/30d12ccdcf5cd7c82df3440618f99264 to your computer and use it in GitHub Desktop.
Save diogobaltazar/30d12ccdcf5cd7c82df3440618f99264 to your computer and use it in GitHub Desktop.
(JAVA) Autocommit: database setting
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydatabase", "username", "pwd");
con.setAutoCommit(false);
statement = databaseConnection.prepareStatement("insert into " + schemaName + "." + tableName + " (" + serializeColumns(columnNames) + ") values(" + serializeValues(columnValues) + ")");
statement.executeUpdate();
databaseConnection.commit();
@diogobaltazar
Copy link
Author

@stackoverflow: Autocommit is a database connection setting that automatically begin and commits a databse transaction whenever a DML statement (UPDATE, INSERT or DELETE) is executed. This simplifies certain programming tasks and reduces the required boilerplate for simple db operations. However, if you are running multiple dependent statements and you want to make sure they all succeed (i.e. if any of them fail or throw an exception then none of them take effect) you will need to turn autocommit off and manually begin/commit the transactions yyourself, which is also more efficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment