× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



        Well... connections aren't thread safe. Your data source should
be handing out a unique connection for each thread that requests one.
getConnection should be a static synchronized method, or contain some
other synchronization method to prevent more than one thread from using
the same connection at the same time.

        I do think your DatabaseMetaData variable is not thread safe.
There will be only one instance of your servlet being used by all
threads (requests). Since dmd is a class variable, and you modify it
from within the performTask method everytime, it will get overwritten
with subsequent requests while previous requests may still be using it.
This could cause issues.

        I'm not sure that answers your question, but that's what I
noticed.

Dan Feather
-----Original Message-----
From: java400-l-bounces@xxxxxxxxxxxx
[mailto:java400-l-bounces@xxxxxxxxxxxx] On Behalf Of RPower@xxxxxxxxxx
Sent: Thursday, April 27, 2006 1:41 PM
To: Java Programming on and around the iSeries / AS400
Subject: Connection Pooling

Question.
I have the following servlet:
/*
 * Created on Mar 18, 2005
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.csj.bulkgarbage;

import java.io.IOException;
import java.sql.*;
import java.util.*;
import javax.naming.InitialContext;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.DataSource;

import com.csj.bulkgarbage.util.*;

/**
 * @author RPower
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class CancelGarbage extends HttpServlet {
        /************************************
        * Put in the parameters and variables 
        * for datasource. 
        * 
        * @author RPower Jun 2, 2005
        ************************************/
        private static DataSource ds = null;
        private static String collectionName;
        private static String garbschedmstTable;
        private String dsname;
        private DatabaseMetaData dmd;
        private ServletContext context;
        private String jdbc_source;

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

        public void doPost(HttpServletRequest req, HttpServletResponse 
resp)
                throws ServletException, IOException {
                performTask(req, resp);
        }

        private void getDS() {
                try {
                        Hashtable parms = new Hashtable();
                        parms.put(
                                "java.naming.factory.initial",
 "com.ibm.websphere.naming.WsnInitialContextFactory");
                        InitialContext cxt = new InitialContext(parms);
                        ds = (DataSource) cxt.lookup("jdbc/" + dsname);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public void init(ServletConfig config) throws ServletException {
                super.init(config);
                try {
                        context = getServletContext();
                        garbschedmstTable =
 context.getInitParameter("bulk.garbmastertable");
                        collectionName = 
context.getInitParameter("pubwrk.lib");
                        dsname = context.getInitParameter("ds.name");
                } catch (Exception e) {
                        System.err.println(e);
                }
        }

        public void performTask(HttpServletRequest req, 
HttpServletResponse resp) {
                HttpSession sess;
                Connection conn = null;
                Customer customer;
                String type;
                String date;
                String counter;
                String url;
                try {
                        sess = req.getSession(true);
                        //!---New code put in Jun 2, 2005
                        if (ds == null)
                                getDS();
                        conn = ds.getConnection();
                        dmd = conn.getMetaData();
                        // -----End New Code------->
                        BulkGarbageSchedMaster b = 
(BulkGarbageSchedMaster) sess.getAttribute("bBulkGarbage");
                        customer = (Customer) 
sess.getAttribute("customer");
                        RecordLock.unlock(ds, conn, collectionName, 
garbschedmstTable, "WGSCHDID", b.getGarbageId(), "WGINUSBY", "WGINUSTM",

req.getRemoteUser(), false);
                        req.setAttribute("newCustomer", "no");
                        sess.setAttribute("customer", customer);
                        sess.setAttribute("disabled", "disabled");
                        if (req.getParameter("counter")!=null){
                                counter = req.getParameter("counter");
                                date = req.getParameter("date");
                                type = req.getParameter("type");
                                url = "Edit_Pickupdate_Apps?type=" +
type 
+ "&date=" + date + "&counter=" + counter;
                        } else {
                                url = "LoadCustomer?" + 
customer.getCustId();
                        }
 getServletConfig().getServletContext().getRequestDispatcher(
                                url).forward(
                                req,
                                resp);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}

When it's being run on the server, sometimes I get the error that the 
connection is closed exception.  Basically, I think that the threads are

modifying each other as they are running.  How do I make the connection 
thread safe?

Ron Power
Programmer
Information Services
City Of St. John's, NL
P.O. Box 908
St. John's, NL
A1C 5M2
709-576-8132
rpower@xxxxxxxxxx
http://www.stjohns.ca/
________________________________________________________________________
___
Success is going from failure to failure without a loss of enthusiasm. -

Sir Winston Churchill





This email communication and accompanying documents is intended only for
the individual or entity to which it is addressed and may contain
information that is confidential, privileged or exempt from disclosure
under applicable law. Any use of this information by individuals or
entities other than the intended recipient is strictly prohibited. If
you have received this in error, please notify the sender and delete all
the copies (electronic or otherwise) immediately.

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.