• Post Reply Bookmark Topic Watch Topic
  • New Topic

Transactions - JTA or JDBC?  RSS feed

 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I've only been doing beans and stuff for a month or so, and I need someone to give me a bit of advice on what to try & what to avoid.
I need transactions and it seems there's a choice between JDBC or JTA transactions (not to mention EJB containers).
I would like to have a seperate bean to do all the database access and error checking and so forth.
With JDBC, it seems that I would have to create the connection object in my servlet and pass it into the data access bean, since the connection object controls the JDBC transaction.
Is passing a connection object like that slow?
Am I right to encapsulate my database access routines in a bean and just call it each time with a new SQL statement?
I'm also worried about threading issues - the connection object and the data access bean would have to be instantiated as local variables in my servlet to be thread-safe, correct?
Or should I use javax.transaction & JTA? I would like to stick with mySQL - does the driver allow JTA transactions?
Thanks to anyone who can answer any of these questions!
Best regards
Adam
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need transactions and it seems there's a choice between JDBC or JTA transactions
should I use javax.transaction & JTA? I would like to stick with mySQL - does the driver allow JTA transactions?

I think both are related. I am not sure about MySql supporting JTA, I trired to access documentation of MySql, but unfortunately right now i cannot access it. May be you should try to find out. Then coming to JDBC, MySql does not support all the features of JDBC, i guess, based on your decision that you want to stick to MySql then your application requiremnts must be able to work with the MySql features.
I would like to have a seperate bean to do all the database access and error checking and so forth.
Am I right to encapsulate my database access routines in a bean and just call it each time with a new SQL statement?

Thats a good idea, you can even go for DAO's design pattern, to implement the data access features which will help you to migrate to other databases in future..
With JDBC, it seems that I would have to create the connection object in my servlet and pass it into the data access bean, since the connection object controls the JDBC transaction.
I am not sure about this, but you can use Connection Pool and get good results.
I'm also worried about threading issues - the connection object and the data access bean would have to be instantiated as local variables in my servlet to be thread-safe, correct?
Instantiating connection object as a local varaiable in Servlet does not guarantee thread safety of resources access. You have to explicitely manage the thread safety of resource access and its specified in Servlet specification. Declaring as local variables help you over come lots of multithreading issues related to servlets.
I would love to hear more comments.
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ram Dhan
I have to clear up the threading issue - I'm pretty sure that I can keep connections thread-safe if I keep them in a bean's instance / member variable.
But the real question is, what do I do with my connection object, if I'm controlling a transaction with it?
NB I checked out the mySQL docs and they have v. little on pooling, but there was some stuff on the seperate site for the mySQL JDBC driver and I think it allows connection pooling, but I can�t tell if transactions are supported. I�ve asked on mySQL list.
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adam,
I have to clear up the threading issue - I'm pretty sure that I can keep connections thread-safe if I keep them in a bean's instance / member variable.
I am not trying to raise a issue, but unless implement implicit thread safety restrictions using isolation levels like SYNCHRONIZED, REPEATABLE_READ etc. on your database connection or synchronize the code blocks you are trying to access the database, your servlet won't be thread safe.

But the real question is, what do I do with my connection object, if I'm controlling a transaction with it?
Regarding this, waht i can suggest is, you refer struts fremework for web application implementation. Even SUN recommends following the STRUTS frame work. You can find the framework user guide here http://jakarta.apache.org/struts/userGuide/index.html
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I'll check out struts to see what they do with the data access parts.
Re the thread-safe stuff, I know about synchronized, but if I keep a connection in a data access bean that has been instantiated from the servlet that needs data, then why do I need synchronized?
Surely each HTTP request gets one instance of the servlet, and each servlet instance instantiates a data access bean, and each instance of the data access bean instantiates a connection?
Why do I have to synchronize access to the connection object?
Thanks
Adam
 
mustang india
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,
dose ur servlet implements single thread model? unless it dose it uses the same instance for all requests.
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rats. I guess I half suspected that. So how do I make sure that my one connection is only used to deal with one incoming HTTP?
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Its highly discouraged to use single threaded model. if you think you have very low hit volume on your server you can go for single threaded model. Implementing single threaded doesn't solve all the problem, its basically used, if you are too bad in writing thread safety servlets.
For protecting your resources being accessed my multiple threads, put your resource accessing code in synchronized block and its the best way to do it.
Hi adam, i don't think you have to care about one connection is only used for each incoming request. If you declare your connection as a local variable, it will be accessed by oneincoming request and all you have to do is make sure that the code which actually access the resource and gets the data is thread safe! Am i able to make my point!
Correct me if i am wrong!
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ram Dhan Yadav K:
If you declare your connection as a local variable, it will be accessed by oneincoming request and all you have to do is make sure that the code which actually access the resource and gets the data is thread safe! Am i able to make my point!

OK, the point�s clear, but then that means I have to pass the connection object as a parameter into my data access bean each time from the servlet that is doing the transaction. Is this something that should be avoided?
I was looking at Struts to see how they do it but at first glance I can�t work it out.
Adam
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adam,
Here is a link to online core servlets book JDBC implementation Core Servlets & JSP JDBC chapter, they have good implementation using servlets altough at the end of the chapter.
You have complete online book at Core Servlets & JSP
I guess you can use the model based on your requirement.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!