Last active
          December 30, 2017 03:07 
        
      - 
      
 - 
        
Save diogobaltazar/30d12ccdcf5cd7c82df3440618f99264 to your computer and use it in GitHub Desktop.  
    (JAVA) Autocommit: database setting
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Class.forName("org.postgresql.Driver"); | |
| con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydatabase", "username", "pwd"); | |
| con.setAutoCommit(false); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | statement = databaseConnection.prepareStatement("insert into " + schemaName + "." + tableName + " (" + serializeColumns(columnNames) + ") values(" + serializeValues(columnValues) + ")"); | |
| statement.executeUpdate(); | |
| databaseConnection.commit(); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
@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.