Solved

transactions in  jsp

Posted on 2003-11-12
13
480 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

861 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