Ropardo Sowftware development company

Experience software development with ROPARDO S.R.L.

RSS Feed
RSS Feed
  • Home
  • About ROPARDO S.R.L
  • Our websites

SQL Transactions in Java

1. Rollback all data on import from a table if a row is corrupted.You have a list of sql queries as a parameter:
- create database connection:

Connection connection = Database.getInstance().getConnection();

- set autocommit on false :

connection.setAutoCommit(false);

(read note1*)
- declare a boolean variable that will be set on true if all is going according to the plan and no errors found

boolean executed = true;

- execute queries and ‘find’ out what worked and what failed:

try {
for(String sql : listOfSQLQueries) {
result = stmt.executeUpdate(sql);
if(result == 0) {
executed = false;
break;
} else {
executed = true;
}
}
} catch (Exception e) {
executed = false;
}
 
// if error found, rollback:
if (!executed) {
connection.rollback();
} else {
connection.commit();
}

*Note1:remember that statement.close() will ignore the connection.setAutocommit(false) and will execute and commit the sql.

2. Rollback using Savepoints:

Savepoint savePoint = null;
try {
savePoint = connection.setSavepoint("dummySavepoint");
dummy_sql = "SELECT * FROM " + databaseSchema + "." + table + " WHERE " + databaseSchema + "." + tableName + "."
+ id + " = '" + id + "'";
// execute logic
}
// catch block
...
connection.rollback("dummySavepoint") // to rollback to our above declared savepoint
connection.releaseSavepoint("dummySavepoint") // to release the savepoint</span>

Note2: A simple rollback or commit erases all savepoints.

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
Get Shareaholic
Tags: Java rollback savepoints SQL transactions

 Posted in: Java
June 11, 2010 | Gabriela Radu | 4 Comments

4 Responses

  • WP Themes
    June 30, 2010
    1

    Nice brief and this enter helped me alot in my college assignement. Thanks you on your information.

  • javaexpert
    July 13, 2010
    2

    You say: ‘set autocommit on false ‘ yet you set it to ‘true’ (connection.setAutoCommit(true);). Actually it should be done like this: first you set autocommit to false and right after you do the connection.commit(); you set it to true again:

    Connection connection = Database.getInstance().getConnection();
    - set autocommit on false :
    connection.setAutoCommit(false);

    boolean executed = true;

    try {
    for(String sql : listOfSQLQueries) {
    result = stmt.executeUpdate(sql);
    if(result == 0) {
    executed = false;
    break;
    } else {
    executed = true;
    }
    }
    } catch (Exception e) {
    executed = false;
    }
    - if error found, rollback:
    if (!executed) {
    connection.rollback();
    } else {
    connection.commit();
    connection.setAutoCommit(true);
    }

  • javaexpert
    July 13, 2010
    3

    You say: \’set autocommit on false \’ yet you set it to \’true\’ (connection.setAutoCommit(true);). Actually it should be done like this: first you set autocommit to false and right after you do the connection.commit(); you set it to true again:

    Connection connection = Database.getInstance().getConnection();
    - set autocommit on false :
    connection.setAutoCommit(false);

    boolean executed = true;

    try {
    for(String sql : listOfSQLQueries) {
    result = stmt.executeUpdate(sql);
    if(result == 0) {
    executed = false;
    break;
    } else {
    executed = true;
    }
    }
    } catch (Exception e) {
    executed = false;
    }
    - if error found, rollback:
    if (!executed) {
    connection.rollback();
    } else {
    connection.commit();
    connection.setAutoCommit(true);
    }

  • javaexpert
    July 13, 2010
    4

    You say: set autocommit on false yet you set it to true (connection.setAutoCommit(true);). Actually it should be done like this: first you set autocommit to false and right after you do the connection.commit(); you set it to true again:

    Connection connection = Database.getInstance().getConnection();
    - set autocommit on false :
    connection.setAutoCommit(false);

    boolean executed = true;

    try {
    for(String sql : listOfSQLQueries) {
    result = stmt.executeUpdate(sql);
    if(result == 0) {
    executed = false;
    break;
    } else {
    executed = true;
    }
    }
    } catch (Exception e) {
    executed = false;
    }
    - if error found, rollback:
    if (!executed) {
    connection.rollback();
    } else {
    connection.commit();
    connection.setAutoCommit(true);
    }

Leave a Reply

 


  • « Previous post
  • Next post »
  • Recent Posts

    • Installing PyGraphviz on Windows
    • Convert python object to XML representation
    • Liferay Portlet Development
    • Norway Road Show 2011 private meeting invitation
    • Oracle OpenWorld 2011
  • Ropardo is Hiring

  • Subscribe

    • Add to Google Reader or Homepage Add to netvibes TopOfBlogs
  • Recent Comments

    • Rajkumar Pomaji on Bluetooth PC Remote Control
    • Stelian Morariu on GWT 2.1 – Uploading a file using the RPC mechanism
    • Sergio on GWT 2.1 – Uploading a file using the RPC mechanism
    • Artem on Liferay: Deployment will start in a few seconds… and how to realy start
    • rkd80 on GWT 2.1 – Uploading a file using the RPC mechanism
  • Archives

    • November 2011 (1)
    • September 2011 (4)
    • July 2011 (3)
    • June 2011 (2)
    • May 2011 (4)
    • April 2011 (4)
    • March 2011 (3)
    • February 2011 (2)
    • January 2011 (2)
    • December 2010 (1)
    • November 2010 (4)
    • October 2010 (4)
    • August 2010 (3)
    • July 2010 (3)
    • June 2010 (6)
    • May 2010 (8)
    • April 2010 (7)
    • March 2010 (9)
    • February 2010 (6)
    • January 2010 (5)
    • December 2009 (7)
    • November 2009 (9)
    • October 2009 (10)
    • September 2009 (14)
    • August 2009 (10)
    • July 2009 (1)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (1)
    • March 2009 (1)
    • October 2008 (3)
    • October 2007 (3)
    • July 2007 (4)
    • June 2007 (1)
    • May 2007 (3)
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Categories

    • News (15)
    • Ropardo Team (8)
    • Ropardo Products (6)
      • File Tracking Client (4)
      • iManagement (2)
    • Software Development (83)
      • Microsoft.NET (22)
      • Java (40)
      • Oracle (8)
      • Power Builder (3)
      • Liferay (5)
      • Lotus Notes (9)
      • xWiki (4)
    • System Adminstration (13)
      • Linux (10)
      • Windows (3)
    • Programming (1)
    • Uncategorized (3)
    • Databases (10)
      • MSSQL (5)
      • PostgreeSQL (3)
    • Microsoft.NET (1)
    • Web Development (28)
      • ASP/ASPX (3)
      • Content Management Systems (1)
      • HTML/CSS (5)
      • Javascrip/AJAX (8)
      • PHP (7)
    • Oracle E Business Suite (6)
  • Tags

    .NET ajax blog C# certification client CMS control css database Debugging django Domino Eclipse extension file tracking filter fun gentoo google Hibernate how to html image iManagement import Java javascript jQuery liferay Linux Lotus Notes lotus script Oracle Oracle BI Publisher 11g PHP portal PostgreSQL powerbuilder Python SQL Telerik velocity xml Xwiki

© 2010 ROPARDO s.r.l..

Powered by WordPress. Styled by Ropardo