• Post Reply Bookmark Topic Watch Topic
  • New Topic

Transactions with JSP ?

 
Sirish Kumar Gongal Reddy
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can you plz tell me how can i proceed with Transactions in JSP like JDBC ?
Thanks,
G Sirish Reddy.,
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strictly speaking, transactions and JDBC code have no place existing in a JSP. Good design states you should separate presentation, business and persistence code into separate parts.

But that said, Transactions work in JSP's the same as they do outside. Kind of. I assume you're talking about managing Transactions via the SQL Connection and not for example via EJB layers or UserTransactions?
 
Sirish Kumar Gongal Reddy
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
Let me thank first.My Question is if i have two seperate JSPs i beging transaction with my first JSP and then First JSP will be forwarded my request to second JSP,second jsp will perform some operation and send the result to first JSP by using this result i need to commit my transaction or rollback my transaction,for the above context i need to maintain some trasaction information(like transaction status or some thing like this) between two jsps right?
How can i maintin in a JSP?Hope that you got my Question.
Regards,
G Sirish Reddy.,
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you insist on managing transactions in a JSP -and David has already pointed out that that is a bad idea-, keep all the code in one page. Handling exceptions will be much harder if need to keep track of state across pages.
 
Daniel Rhoades
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
G Sirish, EJB Session and Entity beans were designed to tackle your exact problem of transaction management.

If there is no way you can use EJBs then look to introduce a controller servlet and a POJO model (following the MVC approach), then you could create a Transfer Object to hold the data, manipulate that object using the servlet to call methods on the model, use the JSP as a view - then either commit the object's properties to the database or delete it using the servlet to call methods on the model. However, there are many problems you need to deal with when using Transfer Objects (i.e. creating Factories, stale data...), most of which the EJB designers have already solved.
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if
1. the user submits jsp-form1,
2. your app locks some stuff in the db
3. your app redirects the user browser to jsp page 2
4. In the meantime the user instead of working on jsp-form2 does something very different.
Problem is that the stuff in the db will remain locked. And this is no good idea. Don't know exact name (contention?). But you should keep those database locks for as short as possible -> 1 request - response cycle.
Else it depends on the good will of your users that certain things in the database won't be locked and this is not a robust basis for your app.

You might save the "unfinished data" the user enters in jsp-1 in a special table of the database or flag those tuples with a not-ready-flag. But I very believe that database locks are simply no good tool for this usage.

I remember that in the literature sometimes the term "application transaction" as oposed to database transaction is mentioned for exact the situation you are describing (if i remember right).

Axel
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do this using some object-relational mapping tools (we have a separate forum for these questions), it is called 'long transactions'. There are a few ways of doing it, but one way is to set a value from the first JSP, then set the values from the second JSP only if the value is still the same. If not your application dies a horrible death and you have to pichk up the pieces by hand.

Possibly a bit over dramatic, bit try to redesign it ito use Ulf's suggestion of a single page. The scenario from Axel isn't just a possibility, it's likely, and if you don't protect against you'll spend all your time trying to manage it.
 
Sirish Kumar Gongal Reddy
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thnaks for your valuable comments.
Thanks,
G Sirish Reddy.,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!