Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Java Beans and Database connection

Hi,
Is there any way by which i can keep my database connectivity in a JavaBean
and make use of this connection in multiple JSP files.
i.e a connection to database is done once and using this connection many
JSP
files interact with the databases.
Regards.
[289 byte] By [vinod] at [2007-11-9 21:19:18]
# 1 Re: Java Beans and Database connection
"vinod" <vinodvishwa@yahoo.com> wrote:
>
>Hi,
>Is there any way by which i can keep my database connectivity in a JavaBean
>
>and make use of this connection in multiple JSP files.
>i.e a connection to database is done once and using this connection many
>JSP
>files interact with the databases.
>
>Regards.

Hello!

The short answer to your question is 'No'. The longer answer to your question
is 'sorta'. There's a couple of ways you could go about this. Depending
upon why you want to do this, the answer could be as simple as connection
pooling. You could take advantage of a JDBC driver that implements connection
pooling, or even roll your own connection pooling mechanism. You could write
your own Connection Manager that only created a single connection and farmed
out a reference to that connection whenever a JavaBean requested one.

The second solution would be to use a servlet that implements the SingleThreadedModel
interface and have it maintain a reference to the Connection object. Then
you could simply have your JSP grab the reference and pass it to the JavaBean.

With a little more information as to what you are trying to do, I could give
you more specific directions.

I hope this is helpful. Happy Coding!

Cordially,

Kyle Gabhart
dev-archive Java Pro
Kyle Gabhart at 2007-11-11 23:05:42 >