Try something like this.  You don't need the dataSource where you have
it and you don't need to synchronize that method. I haven't try this
but it gives you a good start.  If I've missed something I'm sure
someone will chime in.

I would highly recommend reading the Blueprints and getting a good
servlet book - Jason Hunter has a new version just released and I also
I like Core Servlets and Java Server Pages.


public class WhatEverName extends HttpServlet {

public void init(ServletConfig config) throws ServletException{

        super.init(config);
        ServletContext SC = getServletContext( );
   }

public void doGet (HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {

        Connection con = null;
        response.setContentType("text/html");
        PrintWriter out = response.getWriter( );
        HttpSession session = request.getSession(true);
        RequestDispatcher dispatcher;
        //finish code
}
public void databaseRoutine(Connection con, HttpSession session )
throws SQLException {

        String sql = "";
        Statement stmt = null;
        ResultSet rs;
        //finish code

}

public Connection getConnection( ) {
        Connection con = null;
        try{
         Context context = new InitialContext ( );
       javax.sql.DataSource ds = ( javax.sql.DBDataSource )
                  context.lookup ( "DBDataSource" );
             con = ds.getConnection ( );
             }catch ( NamingException n){
              n.printStackTrace();
             }catch ( SQLException s){
              s.printStackTrace();
             }catch ( Exception ex ){
              ex.printStackTrace();
                   }
     return con;
}
public void closeConnection(Connection con ){
     try{
         con.close();
         }catch(SQLException s){
         s.printStackTrace();
        }
   }
}


----- Original Message -----
From: Marc Krisjanous <[EMAIL PROTECTED]>
Date: Sunday, April 22, 2001 7:05 pm
Subject: Bet Practises for Servlets accessing a db - ELP!

> Hello all,
>
> I would like to get feedback on my design for a servlet that is
> required to
> search a db2 database for employees (Yes! the Sample Db!).
>
> The Servlet is configured as follows:
>
> servlet class{
>
> //instance var
> javax.sql.DataSource ds = null;
>
> init(){
>
> //get datasource connection from WebSphere 3.5 (JDBC 2).
> ds = ........
>
> }//end init
>
> doPost(...){
>
> //get a connection from the connection pool
> Connection conn = ds.getConnection(user, password);
>
> //do something then close connection
>
> }//end doPost.
>
> 1. The servlet first establishes a datasource handle to WebSphere's
> connection pool for this database. This is required only once and
> seemedlike a good idea to place in the init method.
>
> 2. For each request (thread) doPost is called and a connection
> object to the
> database is obtained. The question I have is should the
> getConnection method
> call be in a synchronized block such as:
>
> synchronized(objLock){
>
>        Connection conn = ds.getConnection(user, password);
>
> }//end sync block
>
> I think it should not since the connection object (conn) is
> declared within
> the method (thus locale and thread safe) and the getConnection
> method is
> returning a common connection object.
>
> Thoughts and comments please!
>
> Best Regards
>
>
> Marc
>
>
________________________________________________________________________
___
> To unsubscribe, send email to [EMAIL PROTECTED] and include in
> the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http:
> Resources: http://java.sun.com/products/servlet/external-
> resources.htmlLISTSERV 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

Reply via email to