commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: Using BasicDataSource as a static class-variable, is it thread safe?
Date Wed, 24 Sep 2008 12:22:34 GMT
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 <james@carmanconsulting.com> 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 <sinoea@msn.com> 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: user-unsubscribe@commons.apache.org
>  > For additional commands, e-mail: user-help@commons.apache.org
>  >
>  >
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>  For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message