Issues surrounding jsp helper classes and threads

S

Simon Harvey

Hi,

I am hoping that someone could offer some general advice on thread safety
when using helper classes and helper java beans.

In particular I am worried about a javabean that I have made that handles
connection pooling and executing queries.

I decalre the datasource at the top of the class and then use it in a couple
of methods to execute a given statement.

At its simplest, my question is, if this class is used by lots of jsp pages
to get data, is it thread safe?

The other thing is, is there someway to make only one helper object and
share it amongst various requesting pages? At the moment I think that each
page makes a new helper javabean in order to execut its statement. Given
that I want to share connections amongst pages, should I somehow be making
sure that each page that is handling a request is going through only one
instance of the helper class? How should I do this?

Thanks everyone

Kindest Regards

Simon
 
J

John C. Bollinger

Simon said:
Hi,

I am hoping that someone could offer some general advice on thread safety
when using helper classes and helper java beans.

In particular I am worried about a javabean that I have made that handles
connection pooling and executing queries.

I decalre the datasource at the top of the class and then use it in a couple
of methods to execute a given statement.

At its simplest, my question is, if this class is used by lots of jsp pages
to get data, is it thread safe?

That would depend a lot on the details. DataSources should be
thread-safe themselves, but there are all sorts of non-thread-safe
things you could do. Your use of the underlying database is an area
particularly rife with opportunities for thread clashes.
The other thing is, is there someway to make only one helper object and
share it amongst various requesting pages? At the moment I think that each
page makes a new helper javabean in order to execut its statement. Given
that I want to share connections amongst pages, should I somehow be making
sure that each page that is handling a request is going through only one
instance of the helper class? How should I do this?

By using a DataSource obtained via the same JNDI name in the first place
you are sharing connections to the full extent supported by that
DataSource. If you still want all pages in the application to use the
same instance of the bean (to avoid redundant JNDI lookups, for
instance), then set it to application scope everywhere.


John Bollinger
(e-mail address removed)
 
J

Joerg Gippert

[snip]
By using a DataSource obtained via the same JNDI name in the first place
you are sharing connections to the full extent supported by that
DataSource. If you still want all pages in the application to use the
same instance of the bean (to avoid redundant JNDI lookups, for
instance), then set it to application scope everywhere.

Let me add here, that problems also could occur at the underlying database
system. If you have a large application that makes heavy use of concurrent
read/write access to the database, you should use a database system that
supports transactions and use transactions to access the database. That´s
not really a Java thing but I´ve seen people trying to debug their (Java)
code although the database itselfe screwed up things ;)

Just for the record, ;)
regards,
Joerg
 
A

Andy Flowers

Try not to cross post. You have posted the same question in
comp.lang.java.help, albeit with a different title.

Either stick to posting to just one group, or send to multiple
[related/necessary/relevant] groups in the one post.

That way you will get a consistent set of answers in one easy to fetch
place.
 
J

Jon A. Cruz

Andy said:
Try not to cross post. You have posted the same question in
comp.lang.java.help, albeit with a different title.

Actually, that's usually called "multi-posting" or some variant thereof.

Either stick to posting to just one group, or send to multiple
[related/necessary/relevant] groups in the one post.

The latter is "cross-posting". Which is generally to be avoided, however
(as you point out) it's better than multi-posting, though should be
limited in number of groups (as you also pointed out).
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top