The issue here is creating a connection as an instance variable, and
initializing it in the init() method. This would cause problems when
autocommit set to false!

It's okay for connection pooling. But not as an attribute.


Aniket Manohar Shende wrote:
>
> may i know why connections shouldn't be built in the init method . how do
> you manage connection pooling otherwise ??
>
> > ----------
> > From:         Varalakshmi Allamaraju[SMTP:[EMAIL PROTECTED]]
> > Reply To:     A mailing list for discussion about Sun Microsystem's Java
> > Servlet API Technology.
> > Sent:         Monday, May 22, 2000 4:51 PM
> > To:   [EMAIL PROTECTED]
> > Subject:      Re: JDBC connection problem
> >
> > - Never ever try to create connections objects in the constructor/init
> > methods of a servlet. Please refer to any good material on how
> > connections are created, and their relation to transactions!
> >
> > - You mention that you make sure that only one request can access the
> > connectoon variable at a time. I don't see any such magic code in your
> > code.
> >
> > - There have been several discussions on how and where to create
> > connections. Look into the archives. There is also an article in the JDJ
> > March issue.
> >
> >
> > zengjj wrote:
> > >
> > > Hi there,
> > > Now I use the javawebserver2.0 to develope a three-tier Database
> > Applications.
> > > The client want to query the database in the applet through our servlet.
> > > Because it usually take serveral minuters to build a connection, so I
> > use a global Connection variable,
> > > this connection will be built in the init function.
> > > And all request will use this global Connection
> > > variable to query the database. Certainly I ensure the only a request
> > can
> > > aceess the variable at the same time.
> > > I test the servlet using the servletrunner. There are no
> > > problem in all cases.
> > > But when I use the servlet in the javawebserver2.0(trial version) and
> > connect the oracle
> > > database,  I found a problem:
> > >      first query has no problem, but later query will hang forever (do
> > not
> > > return).
> > >
> > > The problem do not happen when connecting other database. If using the
> > > servletrunner to test this servlet, it can connect to all database
> > including the oracle database too.
> > >
> > > My oracle version is 7.3.3 in the WindowsNT.
> > >
> > > Following is my code segment:
> > >
> > > public class MapdPHServlet extends HttpServlet
> > > {
> > > Connection connection ;
> > >
> > > public MapdPHServlet()
> > > {
> > >   super () ;
> > > try
> > >    {
> > >     String URL=new String("jdbc:odbc:testdb");
> > >     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
> > >     connection = DriverManager.getConnection(URL);
> > >    }
> > >    catch (Exception e) {System.out.println ("# Initialize Error : " +
> > >                          e.toString()) ;} ;
> > > }
> > >
> > > public void init (ServletConfig config) throws ServletException
> > > {
> > >    super.init (config) ;
> > >    System.out.println (" # Initalize MapdPH Servlet...") ;
> > > }
> > >
> > > public void destroy()
> > > {
> > >   super.destroy();
> > >   try
> > >   {
> > >     connection.close();
> > >   }
> > >   catch (Exception e) {System.out.println ("# Destroy MapdPH Servlet
> > > Eorror") ;} ;
> > >   System.out.println (" # Destory...") ;
> > > }
> > >
> > > /************* doPost() ***********************/
> > >
> > > protected void doPost (HttpServletRequest req,
> > >                        HttpServletResponse res)
> > >       throws ServletException,IOException
> > >   {
> > > // do a simple query and return the result to the client
> > > // I ensure there are only a request at the same time
> > >   try {
> > >
> > >     Statement      statement=connection.createStatement();
> > >     String SQLStatement = "Select * from person";
> > >     if( statement.execute(SQLStatement) )
> > >     {   //send the result to the client
> > > .....
> > >     }
> > >     else  // with Result Set
> > >     {
> > >     }
> > >
> > >     statement.close();
> > >     }
> > >   catch (Exception e)
> > >     {
> > >       System.out.println (e) ;
> > >     }
> > >
> > >   } // end of doPost()
> > >
> > > Do you think it is my problem or the javawebserver's problem? Can you
> > give
> > > me some
> > > suggestion to deal with the problem? At present, I build the connection
> > in
> > > the doPost and
> > > close the connection before exit the doPost in order to solve this
> > problem.
> > >
> > > By the way, there are some parameters in the javawebserver:
> > > Service Tuning -----  General ------------  Capacity
> > >                             ---  Handler Threads
> > >                             ---  Connection Persistence
> > > I found the javawebserver often hang when there are two many users(about
> > 50)
> > > accessing the server simultaneously if I use the default value of these
> > parameters.
> > > Even if I maximize all parameters, the javawebserver still hang
> > occasionally.
> > > Do you think it is my problem or the javawebserver's problem?
> > >
> > > thanks in advance,
> > >
> > > jianjiang zeng
> > >
> > > ________________________
> > > Are you ChinaRen?
> > > �"Y�-中�>�人�s"�"�子�"Y活
> > > http://www.chinaren.com
> > >
> > >
> > __________________________________________________________________________
> > _
> > > 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
> >
> > __________________________________________________________________________
> > _
> > 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
> >
>
> ___________________________________________________________________________
> 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

--

*****************************************************************
Who are YOU?' said the Caterpillar.

This was not an encouraging opening for a conversation. Alice replied,
rather shyly, `I--I hardly know, sir, just at present-- at least I know
who I WAS when I got up this morning, but I think I must have been
changed several times since then.'

`What do you mean by that?' said the Caterpillar sternly. `Explain
yourself!'

`I can't explain MYSELF, I'm afraid, sir' said Alice, `because I'm not
myself, you see.'

*****************************************************************

___________________________________________________________________________
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

Reply via email to