Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

AutoTransaction rollback?

Thread view: 
getsanjay.sharma@gmail.com - 09 May 2007 18:33 GMT
Hello to all programmers out there.

I am currently developing a Java web application using Servlets / JSP.
I was just wondering how I would go about implementing complete commit
or complete rollback. For eg. I have a controller which depending on
the user input would update more than one tables simultaneously. I
want this operation to either completely commit or rollback. Its
something like this:

private void process(..)
{
   policyDAO.addPolicy(policyBean); //would add a policy record to
the db
   customerDAO.addCustomer(customerBean); // would add a customer
record to database.
   ...
}

Is there any way for me to make changes to db only when all the
additions are successful? Thanks for your help.
Kai Schwebke - 09 May 2007 18:54 GMT
getsanjay.sharma@gmail.com schrieb:
> Is there any way for me to make changes to db only when all the
> additions are successful?

Yes -- have a look at the Sun JDBC tutorial:
http://java.sun.com/docs/books/tutorial/jdbc/basics/transactions.html

Kai
getsanjay.sharma@gmail.com - 09 May 2007 19:15 GMT
> Yes -- have a look at the Sun JDBC tutorial:http://java.sun.com/docs/books/tutorial/jdbc/basics/transactions.html

But it would require me to pass around the connection object and
keeping it alive, not to mention changing the function prototype.
Isn't there an other way of doing it? Thanks for your help.
Kai Schwebke - 09 May 2007 20:43 GMT
getsanjay.sharma@gmail.com schrieb:
>> Yes -- have a look at the Sun JDBC tutorial:http://java.sun.com/docs/books/tutorial/jdbc/basics/transactions.html

> But it would require me to pass around the connection object and
> keeping it alive, not to mention changing the function prototype.
> Isn't there an other way of doing it? Thanks for your help.

The database may be used by many different users (=connections)
in parallel. So a transaction has to be scoped somehow.
This scope is the connection.

Passing the connection around is one of the simplest ways to
achieve thread safety, which is needed in a servlet environment.

Kai
Wojtek - 09 May 2007 20:44 GMT
getsanjay.sharma@gmail.com wrote :
>> Yes -- have a look at the Sun JDBC
>> tutorial:http://java.sun.com/docs/books/tutorial/jdbc/basics/transactions.html
>
> But it would require me to pass around the connection object and
> keeping it alive, not to mention changing the function prototype.
> Isn't there an other way of doing it? Thanks for your help.

So you want to start a transaction, then go back to the user to get
more information, then continue with the transaction, etc?

This is a bad idea. Not only are you holding open a connection to the
database for an unknown but really large amount of time, but you do not
know if the user will get bored and not continue with the process.

Plus if you have many users, you will need many connections.

Change the way you do things. Get ALL the user input first, storing
intermediate stages in the session, then process all the information
into the various tables in one operation.

Signature

Wojtek :-)

getsanjay.sharma@gmail.com - 10 May 2007 02:56 GMT
Thanks a lot for all your input. Normally I shove all this work on to
the App server while developing Entity beans and move along with it.
Passing connection around seemed not such a good idea. Maybe I would
just configure the Datasource in the Tomcat Web server and look into
some docs to see if it leads to something.

Thank you.
Harry - 10 May 2007 03:05 GMT
Hi,

If you're using non-EJB, I think the only way to do transaction is
using connection object as stated above. What I would like to say is
setting Datasource does not change the statement, as this only
provides you with the connection pool, you still need the connection
object to control the transaction.

Regards,
Harry

On May 9, 6:56 pm, getsanjay.sha...@gmail.com wrote:
> Thanks a lot for all your input. Normally I shove all this work on to
> the App server while developing Entity beans and move along with it.
[quoted text clipped - 3 lines]
>
> Thank you.
getsanjay.sharma@gmail.com - 10 May 2007 19:35 GMT
Thanks Harry, would keep that in mind.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.