Solved

transactions in  jsp

Posted on 2003-11-12
13
478 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference between member and local variables. 29 136
reflections, dynamic class loading 3 70
Fisheye tool 2 111
eclipse buid path vs tomcat lib path 10 24
In this article we will learn how to backup a VMware farm using Nakivo Backup & Replication. In this tutorial we will install the software on a Windows 2012 R2 Server.
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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