Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Threading with Database Access

Reply
Thread Tools

Threading with Database Access

 
 
Tommy
Guest
Posts: n/a
 
      05-11-2005
Hi!

I have a singleton class that accesses a database (using jtds as
database driver).

class DatabaseAccessSingleton {

private static DatabaseAccessSingleton instance;

public static DatabaseAccessSingleton getInstance() {
if (instance==null) {
instance = new DatabaseAccessSingleton();
}
return instance;
}

public Vector getData() {
Vector data = new Vector();

try {
ResultSet rs =
connection.createStatement().executeQuery("SELECT * FROM TABLE");

while (rs.next()) {
data.add(rs.getString(1));
}

return data;

} catch (Exception e) {
}
}
}

Thatīs roughly the pattern that I use.
DatabaseAccessSingleton.getData() might be called by multiple threads.
So my question is, how to make it thread-safe?
In the jtds database driver I have read that while connection is
threadsafe, Statement is not.

So if I make the whole method synchronized, I guess that would solve
it. But then, only one thread at a time has access to the database, the
other threads would be blocked.

How can I allow multiple threads to use getData() without blocking? Or
is the method getData() even thread-safe as it is now?

Thanks!

Tommy

 
Reply With Quote
 
 
 
 
Joseph Dionne
Guest
Posts: n/a
 
      05-11-2005
Tommy wrote:
> Hi!
>
> I have a singleton class that accesses a database (using jtds as
> database driver).
>
> class DatabaseAccessSingleton {
>
> private static DatabaseAccessSingleton instance;
>
> public static DatabaseAccessSingleton getInstance() {
> if (instance==null) {
> instance = new DatabaseAccessSingleton();
> }
> return instance;
> }
>
> public Vector getData() {
> Vector data = new Vector();
>
> try {
> ResultSet rs =
> connection.createStatement().executeQuery("SELECT * FROM TABLE");
>
> while (rs.next()) {
> data.add(rs.getString(1));
> }
>
> return data;
>
> } catch (Exception e) {
> }
> }
> }
>
> Thatīs roughly the pattern that I use.
> DatabaseAccessSingleton.getData() might be called by multiple threads.
> So my question is, how to make it thread-safe?
> In the jtds database driver I have read that while connection is
> threadsafe, Statement is not.
>
> So if I make the whole method synchronized, I guess that would solve
> it. But then, only one thread at a time has access to the database, the
> other threads would be blocked.
>
> How can I allow multiple threads to use getData() without blocking? Or
> is the method getData() even thread-safe as it is now?
>
> Thanks!
>
> Tommy
>

http://www.janeg.ca/scjp/threads/synchronized.html

change your getdata method declaration to;

public synchronized Vector getData( ...
 
Reply With Quote
 
 
 
 
shakah
Guest
Posts: n/a
 
      05-11-2005
Tommy wrote:
> Hi!
>
> I have a singleton class that accesses a database (using jtds as
> database driver).
>
> class DatabaseAccessSingleton {
>
> private static DatabaseAccessSingleton instance;
>
> public static DatabaseAccessSingleton getInstance() {
> if (instance==null) {
> instance = new DatabaseAccessSingleton();
> }
> return instance;
> }
>
> public Vector getData() {
> Vector data = new Vector();
>
> try {
> ResultSet rs =
> connection.createStatement().executeQuery("SELECT * FROM TABLE");
>
> while (rs.next()) {
> data.add(rs.getString(1));
> }
>
> return data;
>
> } catch (Exception e) {
> }
> }
> }
>
> Thatīs roughly the pattern that I use.
> DatabaseAccessSingleton.getData() might be called by multiple

threads.
> So my question is, how to make it thread-safe?
> In the jtds database driver I have read that while connection is
> threadsafe, Statement is not.
>
> So if I make the whole method synchronized, I guess that would solve
> it. But then, only one thread at a time has access to the database,

the
> other threads would be blocked.
>
> How can I allow multiple threads to use getData() without blocking?

Or
> is the method getData() even thread-safe as it is now?
>
> Thanks!
>
> Tommy

It might be OK as is, as long as it is safe to interpret "Statement is
not" as meaning that multiple threads calling the same Statement Object
is not thread-safe but that multiple threads using distinct Statement
Objects (as you imply in your example) is OK. I'll throw in the
obligatory disclaimer that I can also see how that interpetation could
be unsafe, given that I tend to equate a Connection with a socket and
that the SQL traffic would have to successfully share that socket.

 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      05-12-2005
On Wed, 2005-05-11 at 14:16 -0700, Tommy wrote:
> Hi!
>
> I have a singleton class that accesses a database (using jtds as
> database driver).
>
> class DatabaseAccessSingleton {
>
> private static DatabaseAccessSingleton instance;
>
> public static DatabaseAccessSingleton getInstance() {
> if (instance==null) {
> instance = new DatabaseAccessSingleton();
> }
> return instance;
> }
>
> public Vector getData() {
> Vector data = new Vector();
>
> try {
> ResultSet rs =
> connection.createStatement().executeQuery("SELECT * FROM TABLE");
>
> while (rs.next()) {
> data.add(rs.getString(1));
> }
>
> return data;
>
> } catch (Exception e) {
> }
> }
> }
>
> ThatÂīs roughly the pattern that I use.
> DatabaseAccessSingleton.getData() might be called by multiple threads.
> So my question is, how to make it thread-safe?
> In the jtds database driver I have read that while connection is
> threadsafe, Statement is not.
>
> So if I make the whole method synchronized, I guess that would solve
> it. But then, only one thread at a time has access to the database, the
> other threads would be blocked.
>
> How can I allow multiple threads to use getData() without blocking? Or
> is the method getData() even thread-safe as it is now?
>
> Thanks!
>
> Tommy
>


I'd be tempted to leave it as-is. Statement may well be unsafe but
you're throwing it away anyway. Outside that I leave the sync/serial to
the DBMS (It's better at it).

Failing that, if you're sharing your connection, why not synchronize on
that throughout?

Ross


--
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + in


 
Reply With Quote
 
Tommy
Guest
Posts: n/a
 
      05-12-2005
One thing that is still not totally clear is the role of local
variables.

In the getData() method, there is the data Vector. So given I would not
make any call to the database via connection, there would not be any
threading issue since data is not shared between threads.
Data is a local variable that gets created when the method is called.
So when different threads call getData(), each gets its own new data
object.

Is that correct? So could I make the local data Vector into an
ArrayList and still consider it thread-safe (ignoring the database
access issue for a while)?

 
Reply With Quote
 
shakah
Guest
Posts: n/a
 
      05-12-2005
Tommy wrote:
> One thing that is still not totally clear is the role of local
> variables.
>
> In the getData() method, there is the data Vector. So given I would

not
> make any call to the database via connection, there would not be any
> threading issue since data is not shared between threads.
> Data is a local variable that gets created when the method is called.
> So when different threads call getData(), each gets its own new data
> object.
>
> Is that correct? So could I make the local data Vector into an
> ArrayList and still consider it thread-safe (ignoring the database
> access issue for a while)?


Sure, all the variables used in getData() (data, the Statement, the
ResultSet) are local (except for connection, of course, which is
presumably an instance variable) and pose no concurrency problems.

 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      05-13-2005
On Thu, 2005-05-12 at 04:26 -0700, Tommy wrote:
> One thing that is still not totally clear is the role of local
> variables.
>
> In the getData() method, there is the data Vector. So given I would not
> make any call to the database via connection, there would not be any
> threading issue since data is not shared between threads.
> Data is a local variable that gets created when the method is called.
> So when different threads call getData(), each gets its own new data
> object.
>
> Is that correct? So could I make the local data Vector into an
> ArrayList and still consider it thread-safe (ignoring the database
> access issue for a while)?
>

This is true. I would make getData() return a Collection (or maybe List
if ordering/indexing matters), and you could then return any kind of
list you like.

The data variable is your new list, you're guaranteed no-one else is
accessing it because you haven't given it to anyone else yet, so you
don't need the overhead of Vector's synchronization (which could be
surprising with a lot of records).

Ross

--
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + in


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Re: threading in PyQt vs threading in standard library Steven Woody Python 0 01-09-2009 07:48 AM
threading in PyQt vs threading in standard library Steven Woody Python 0 01-09-2009 07:15 AM
Cooperative threading preemptive threading - a bit confused failure_to@yahoo.co.uk Java 9 12-29-2007 01:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57