wood burning stoves 2.0*
The moose likes JDBC and the fly likes Transactions and Threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Transactions and Threading" Watch "Transactions and Threading" New topic
Author

Transactions and Threading

Anup Mahale
Greenhorn

Joined: Sep 10, 2003
Posts: 5
Hi all,
I am new to web based programming..
This is my problem:
I have a Jsp page (jsp1) which takes some input from the user (say his name) thru a text field. Now I call a Java class file which stores this name into a database(MySql).
I have another jsp page (jsp2) which displays the names stored in the databse.
Now if two different users are accessing the two jsp pages at the same time, I dont want wrong data to be displayed or a wrong update to be made.
So how do I solve this? I know I have to use transactions but I am not sure how.
And do I need to use threading so that two different users can read data at the same time?
It would be great if I can find some online tutorial.
thanx all.
Bye
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Because the name is user-specific, the two users should not be accessing the same record in the database, and thus you won't need transactions. Why are you inserting the name into a database? Wouldn't storing it in the HttpSession be sufficient?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Anup Mahale
Greenhorn

Joined: Sep 10, 2003
Posts: 5
well, that was just an example I gave..so if I am storing some records in a database and if two different users want to access them at the same time, do I need to use threads or is it enough that I use transactions?
Thanks.
Jay Dellinger
Greenhorn

Joined: Sep 19, 2002
Posts: 12
Your app server will already spin off threads to process each request. Your database will handle multiple accesses so that you don't have to worry about anything like access conflicts, file locking, etc. This is assuming that each page is creating it's own connection to the database, since you didn't mention any controller servlets, etc.
What you really need to protect against is improperly sharing the connection in your pages. Don't use a page declaration to (<%! ... %> to define your connection variable. Multiple threads could access the page at the same time and cause problems. Just create your connection in a scriptlet, use it, then dispose of it.
I don't believe you need to worry about transactions if you are just doing a simple insert. Transactions are useful when you need to guarantee that several database changes occurred succesfully. For example, if you had a banking application, and were transferring money from one account to another, you would want to guarantee that both the increment to one account and the decrement to the other both occurred before committing the transaction. If either one fails you would want to roll back all changes.
Also, you may have simplified your example to better illustrate the point, but if not, you should consider using an MVC pattern and moving your database logic out of your view layer. It makes maintenance and reuse much easier. Of course if you just need something quick and functional...jsps will work.
Hope that helps,
Jay
Kalpesh Soni
Ranch Hand

Joined: Jan 02, 2001
Posts: 312
Ok
I believe that Anup is trying to prevent concurrent updates to the same row
here is the situation
some table some row some column has data "5" in the db
one jsp is capable of showing the data and has a text box, where u can change the data and press the update button
2 guys hit the same jsp
both see 5
one makes it 10, saves it
the other makes it 12, saves it
the first guys sees the updated page and sees 12 !
second guy sees 12 and has no idea that he overwrote someone's changes !!!

the solution is optimistic strategy
transactions wont help here, or they are an overkill

add a column named version number in the table
when jsp selects data it also selects this version number
so the update query looks like this
first guy
update mytable set mycol = 10 where mypk=xxx and version_number = 1
second guy
update mytable set mycol = 12 where mypk=xxx and version_number = 1
now second update query will change no rows
so return int will give 0
voila
we cought him


Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta
SCMAD Beta
SCDJWS Beta

KS
Anup Mahale
Greenhorn

Joined: Sep 10, 2003
Posts: 5
Thanks a million Kalpesh and Jay, you people were great help.
And Jay, I am using Model 1 architecture and the database logic is in a separate layer. I am using a Bean to get my connections. So i need not wory about anything like access conflicts, file locking, etc. right?
Thanks once again guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transactions and Threading