Filippos Slavik wrote:
>
> At 07:04 рм 6/9/2000 -0600, you wrote:
> >Chin Cedric Sung Kit wrote:
> > >
> > > i initialize my oracle-jdbc
> > > database connection in my LoginServlet. how do i re-use this connection
> > > in my other jsp pages, so that i will not have to open so many
> > > connections?
> >
> >You don't re-use your connection. At a minimum, you should use a new
> >connection for every session. If you try to use the same connection for
> >every request, you will have concurrency problems.
>
> This is completely wrong. You can share a single java.sql.Connection
> instance among many threads. Synchronization issues appear in the context
> of java.sql.Statement instances, which again can be shared among many
> threads but with care.
Not at all. With one connection, how do you handle this situation:
Thread 1 -> conn.setAutoCommit(false); ---------------------------->
executeUpdate()
Thread 2 -------------------------------> con.setAutoCommit(true)
Thread 1 sets autocommit to false. Thread 2, using the same Connection
instance, sets autocommit to true. Thread 1 attempts a long set of SQL
statements.
I would also question your claim the Statements can be shared with care.
Maybe it is possible if one is using Statements for executeUpdate which
returns an int value. In that case, you can synchronize those one or two
statements. However, if you are using the Statement to get a ResultSet,
you should use separate Statements. You could synchronize, but that's
the same as doing everything serially. Why should thread 2 wait for
thread 1 to finish using the ResultSet, especially if it's a scrollable
result set? (At least with oracle jdbc 1.0. The Statement object uses
the same ResultSet object for every query.) It's just as easy for thread
2 to get its own Statement object.
> I have personally build a db engine implemented as singleton to be used and
> shared among many servlets running under the same JVM process. The db
> engine opens a certain amount of connection to the database subsystem (2-3
> are just enough) and share them to all running threads, which in the case
> of servlets may be quite a lot!
Oh, you created a Connection Pool, and while connections are shared,
they are not CONCURRENTLY shared. Sure, I agree that connections can be
re-used. But the original question did not ask how to use pooled
connections or how to reuse a connection; the original question asked
how to use a SINGLE connection in multiple JSP servlets.
I sent this to another poster yesterday (I sent it privately, so it will
not be in the list archive):
"If your application is using the most up to date JDBC features, you
should get your connection from a DataSource object. Check the advanced
tutorial at http://java.sun.com/products/jdbc/learning.html for
information about a DataSource. If you can't use a DataSource, then the
next best thing is to use a connection pool. Most servlet books address
connection pools. As a last resort (or if connection creation time is
not an issue), create a new connection for every request to the
servlet."
I still stand by my original post: One should not use a single
Connection for every request to a JSP/servlet.
K Mukhar
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html