What are the Database transactions? How we can maintain transactions using JDBC? Explain with example?

Showing Answers 1 - 12 of 12 Answers

Database transaction are a series of sql statement that causes modification in database.

For example making debit of some amount from someone's account to other person's account. Involves querries of debiting & crediting. If both complete then transaction will commit or will rollback.
 
Have to find about transaction management in JDBC??

  Was this answer useful?  Yes

sampra

  • Feb 11th, 2008
 

Unit of database operation in a single unit and follow all-none preposition is called transation

  Was this answer useful?  Yes

trcdutt

  • Feb 15th, 2008
 

Here is a skeleton of code for implementing a transaction.

import java.sql.Connection;

public class Test {

public void AllTransactionStatements() {
    Connection con = getCon();
    boolean allIsWell =true; // initialize to true and change it if some thing goes wrong in the try block
    try {
        getCon().setAutoCommit(false);
       // list of all the statements that need to be part of transaction
      .......
      ........
      // set the allIsWell to false if something goes wrong
       allIsWell = false;

    }
    catch(Exception ex) {
          allIsWell =false;
      
    }
    finally {
       if (allIsWell )
           getCon().commit();
      
else
          getCon().rollback(); // roll back the transaction as a whole unit with in the     try block

    }
} // end of method

public Connection getCon() {
Connection conn =null;
// method that will return your Connection object
// call your specific methods to get the conn object
  return conn;
}

}

Database transactions => A group of SQL queries which should either all be executed or none should be executed will comprise of a tracnsaction.

Maintain transactions using JDBC => We can do so upto a basic level as somebody already explained above but beyond that, as an application developer you would prefer to use 3 party APIs for transaction management which should be depended on the databases that you are using and the environment [like network / mirrored etc].

Thanks,
Vinay

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions