However, the example public static DataSource getDataSource() is not
thread-safe.

Access to the static variable ds should be synchronized (e.g. by
synch'ing the method) to ensure that different threads see the same
value of ds.

On 24/09/2008, James Carman <[EMAIL PROTECTED]> wrote:
> 1.  Yes it's thread-safe (it kind of has to be).  There should only be
>  one instance of BasicDataSource for your application.
>
>  2.  Do not create a new copy every time (and it shouldn't if you're
>  looking it up in JNDI).  That would defeat the purpose of having a
>  pool (as you pointed out).
>
>
>  On Wed, Sep 24, 2008 at 4:06 AM, sinoea kaabi <[EMAIL PROTECTED]> wrote:
>  >
>  > Dear all,
>  > I am using the commons-dbcp BasicDataSource with Tomcat 5.5 configured via
>  > JNDI.
>  >
>  > I use a Data class as a datasource manager to retrieve the datasource from 
> a static method.
>  >
>  > The datasource (instance of BasicDataSource) is a static class-variable of 
> the Data class.
>  >
>  > See Code below:
>  >
>  > [code]
>  > public class Data {
>  >
>  >    private static BasicDataSource ds = null;
>  >
>  >
>  >    public static DataSource getDataSource() throws SQLException {
>  >        if (ds == null) {
>  >            try {
>  >                final Context initContext = new InitialContext();
>  >                ds = 
> (BasicDataSource)initContext.lookup("java:/comp/env/jdbc/myDB");
>  >                initContext.close();
>  >                return ds;
>  >            } catch (final NamingException e) {
>  >                e.printStackTrace();
>  >                throw new RuntimeException("Java naming exception when 
> getting connection from tomcat pool: " + e.getMessage());
>  >            }
>  >        } else {
>  >
>  >            return ds;
>  >        }
>  >  }
>  > [/code]
>  >
>  > Question:
>  >
>  > 1. Is it thread safe to have the datasource (ds) as a static class variable
>  >
>  > Or if I create a new datasource for each method call as below:
>  >
>  > [code]
>  > public class Data {
>  >
>  >    public static DataSource getDataSource() throws SQLException {
>  >
>  >            try {
>  >                final Context initContext = new InitialContext();
>  >                final BasicDataSource ds = 
> (BasicDataSource)initContext.lookup("java:/comp/env/jdbc/myDB");
>  >                initContext.close();
>  >                return ds;
>  >            } catch (final NamingException e) {
>  >                e.printStackTrace();
>  >                throw new RuntimeException("Java naming exception when 
> getting connection from tomcat pool: " + e.getMessage());
>  >            }
>  >
>  >  }
>  > [/code]
>  >
>  > Does that mean I am creating a new pool for each method call?
>  >
>  > Thanks for any help!
>  > _________________________________________________________________
>  > Win New York holidays with Kellogg's & Live Search
>  > http://clk.atdmt.com/UKM/go/111354033/direct/01/
>  > ---------------------------------------------------------------------
>  > To unsubscribe, e-mail: [EMAIL PROTECTED]
>  > For additional commands, e-mail: [EMAIL PROTECTED]
>  >
>  >
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: [EMAIL PROTECTED]
>  For additional commands, e-mail: [EMAIL PROTECTED]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to