Solved

transactions in  jsp

Posted on 2003-11-12
13
475 Views
Last Modified: 2012-05-04
I have a jsp page which contains a number of sql statements say 2 inserts,
1 delete and 1 update. They are a part of one transaction. So I want either none is executed or all of them r executed. How do I make sure that this happens. Do i need to do something in the jsp code to ensure or will my database(mysql) handle this.
pz suggest
0
Comment
Question by:will_789
  • 7
  • 4
  • 2
13 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 170 total points
ID: 9730598
<%
   connection.setAutoCommit( false ) ;
   try
   {
     // do an update

     // do an insert
 
     // do a delete

     connection.commit() ;
   }
   catch( Exception ex )
   {
      ex.printStackTrace() ;
      connection.rollback() ;
   }
%>

Assuming your database allows commit and rollback :-)
0
 
LVL 1

Assisted Solution

by:JNic
JNic earned 165 total points
ID: 9730605
You should use InnoDB tables which is supported by mySQL from version 3.23.34a.

Read more about them here: http://www.mysql.com/doc/en/InnoDB.html

Then you will be able to do things like the following:

// assuming you have a connection
 connection.setAutoCommit(false);
 connection.setTransactionIsolation(4);
 statement = connection.createStatement();
  for (int i=0; i<10; i++){
      String update="Insert into numbers (number) values (i)";
      statement.executeUpdate(update);
   }
   boolean doIt=false;
   // Then do some testing to see if you want the changes or not.
  if (doIt){
      statement.commit(); // this executes all the updates
  }
  else{
     statement.rollback(); // this eleminates all the updates
  }
}

If my memory serves me right, a SQLException will do a rollback automatically. - Other Exceptions will not.

Hope it helped!

Regards,

Nic


 

0
 
LVL 1

Expert Comment

by:JNic
ID: 9730612
Oh - I'm too slow ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730635
Hehehe ;-)

Not sure if he's using MySQL either...

Databases like Postgres or Oracle can rollback anyway (no special table type needed)

And you have to watch out if you are using something like MS Access, as turning auto-commit off will have a HUGE performance hit

(as it will lock an entire db page till you commit or rollback, not a single row as with the others)

Tim.
0
 
LVL 1

Expert Comment

by:JNic
ID: 9730658
Quote from TimYates: Not sure if he's using MySQL either...
Quote from question: "my database(mysql)"

MUAHAHAHAHAHAHAHAAAAA!!!!
;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730669
Whoops! >_<

hehehehehehehe ;-)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:will_789
ID: 9730673
Could u plz tell me what's the purpose of

connection.setTransactionIsolation(4);
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730705
I think the 4 is:

Connection.TRANSACTION_SERIALIZABLE

Which means:

--------

A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented. This level includes the prohibitions in TRANSACTION_REPEATABLE_READ and further prohibits the situation where one transaction reads all rows that satisfy a WHERE condition, a second transaction inserts a row that satisfies that WHERE condition, and the first transaction rereads for the same condition, retrieving the additional "phantom" row in the second read.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730718
Whoops!

Connection.TRANSACTION_REPEATABLE_READ == 4 ;

A constant indicating that dirty reads and non-repeatable reads are prevented; phantom reads can occur. This level prohibits a transaction from reading a row with uncommitted changes in it, and it also prohibits the situation where one transaction reads a row, a second transaction alters the row, and the first transaction rereads the row, getting different values the second time (a "non-repeatable read").
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730728
it means that other people reading the database will not see different data until the transaction has been committed
0
 
LVL 1

Expert Comment

by:JNic
ID: 9730744
Oops I actually did not mean to write that line:

connection.setTransactionIsolation(4);

like that.

This method has four different settings:

connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

Read more about what the settings mean here:

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Connection.html

Look under FIELD DETAIL
0
 

Author Comment

by:will_789
ID: 9730752
thanks a lot
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730763

TRANSACTION_READ_COMMITTED

or

TRANSACTION_REPEATABLE_READ

Are usually the best ones to use...  It's a matter of personal choice, and whether you want repeatable reads...
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
A procedure for exporting installed hotfix details of remote computers using powershell
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now