AutoTransaction rollback?

  • Thread starter getsanjay.sharma
  • Start date
G

getsanjay.sharma

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.
 
K

Kai Schwebke

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
 
W

Wojtek

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.
 
G

getsanjay.sharma

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.
 
H

Harry

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top