commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sinoea kaabi <sin...@msn.com>
Subject RE: Using BasicDataSource as a static class-variable, is it thread safe?
Date Wed, 24 Sep 2008 12:41:13 GMT

Right, so you all agree that the datasource should be retrieved from one single class, rather
than implementing a getDataSource() method in every Dao (because then you would create a new
pool for each method call).

If that is the case ( that the datasource should be retrieved from one single class ) then
the 
method has to be static.

It would not make sense to make the method non-static, then the code would be like this:
new Data().getDataSource()

Again, you would be creating new pools of each method call to a non-static method (public
DataSource getDataSource()),
because you are creating  a new object of the Data class.

And as you say, the method must be synchronized to ensure that different threads see the same
value of datasource.

so the final code would look like this:

[code]
public class Data {

private static BasicDataSource ds = null;

 public static synchronized  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]

Is that correct?


> Date: Wed, 24 Sep 2008 13:22:34 +0100
> From: sebbaz@gmail.com
> To: user@commons.apache.org
> Subject: Re: Using BasicDataSource as a static class-variable, is it thread safe?
> 
> 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
> 

_________________________________________________________________
Win New York holidays with Kellogg’s & Live Search
http://clk.atdmt.com/UKM/go/111354033/direct/01/
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message