Static member variable - Singleton Pattern required?

D

downlode

Hi,

Just trying to get my head round the behaviour of static variables.

I have a class in my web application which I use to get a database
datasource with connection pooling. The datasource is a static member
of the class.

In the function getDataSource() below, will all steps be executed if
the static dataSource member has already been initialised?
Or will they be bypassed, and the dataSource member simply returned?

In other words -
Is the code below OK, or:
Should I just put a test in getDataSource() to see if dataSource ==
null, and if so initialise it. Otherwise simply return it. Or:
Should I initialise dataSource with a singleton pattern?


Thanks,
Mike
//////////////////////////////////////

import javax.sql.*;
import java.util.*;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.DataSourceConnectionFactory;
import org.apache.commons.dbcp.DriverConnectionFactory;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.BasicDataSource;

public class DatabaseUtil {

static PoolingDataSource dataSource = null;

// Returns a DataSource that implements connection pooling
public static DataSource getDataSource(String configDir) throws
Exception {


String url = "/* hardcoded for testing */";
String driver = "/* hardcoded for testing *";
BasicDataSource ds = new BasicDataSource();
ds.setUrl(url);
ds.setDriverClassName(driver);
ds.setUsername("user");
ds.setPassword("pass");
ds.setMaxActive(15);

ObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new
DataSourceConnectionFactory(ds);
PoolableObjectFactory poolableConnectionFactory = new
PoolableConnectionFactory(connectionFactory, connectionPool, null,
null, false, true);
dataSource = new PoolingDataSource(connectionPool);

return dataSource;
}
}

//////////////////////////////////////
 
J

jan V

import javax.sql.*;
import java.util.*;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;

Any reason why you don't use the .* form for the dbcp and pool packages? A
consistent style helps in the long run..
public class DatabaseUtil {

static PoolingDataSource dataSource = null;

Do you mean

private static PoolingDataSource dataSource = null;

If not, why do you declare this static field package scope?
// Returns a DataSource that implements connection pooling
public static DataSource getDataSource(String configDir) throws
Exception {

"throws Exception" is a very bad idea. You need to be more specific,
otherwise you'll sweep up exceptions like NullPointerException which
indicate BUGS, not exceptional program states...
 
C

Chris Uppal

In the function getDataSource() below, will all steps be executed if
the static dataSource member has already been initialised?
Or will they be bypassed, and the dataSource member simply returned?

I'm having difficulty seeing why your think the steps might be bypassed.
There's nothing "magic" about static methods -- they are just code like
anything else. If someone calls that method three times, then the steps it
defines will be executed three times.

You /can/ arrange for code to be executed once, automatically, as a class is
intialised -- which is perhaps what you are thinking of -- but that is done by
using the syntax:

class MyClass
{
static
{
// code that executes as class is initialised
}

//... other stuff
{

So, yes, you need to include an explicit test in getDataSource(), or arrange in
some other way that it will only ever be called once.

BTW, you should probably make that method synchronized for real use.

-- chris
 
D

downlode

Hi Chris,
The main reason for confusion is that someone else said the code was
OK. So I thought perhaps I had misunderstood something.
As you point out though, all the code in the function will be executed
every time it's called, which isn't what I want - presumably it would
mean that the dataSource variable would be reset every time the
function was called.
Thanks for clarifying.
Mike
 
H

Hemal Pandya

jan V wrote:
[....]
"throws Exception" is a very bad idea. You need to be more specific,
otherwise you'll sweep up exceptions like NullPointerException which
indicate BUGS, not exceptional program states...

How will being specific help, especially against NullPointerException?
 
J

jan V

"throws Exception" is a very bad idea. You need to be more specific,
How will being specific help, especially against NullPointerException?

No throws clause should ever specify NullPointerException, either
explicitly, or implicitly. During development you *want* these kinds of
exceptions to blow up in your face so that you're left in no illusion that
you've discovered a bug.
 
H

Hemal Pandya

No throws clause should ever specify NullPointerException, either
explicitly, or implicitly. During development you *want* these kinds of
exceptions to blow up in your face so that you're left in no illusion that
you've discovered a bug.

I can't figure it out. I am sure I am missing something and will
appreciate clarification.

Most often, the callers of a method "throws Exception" themselves in
turn "throws Exception" and the NullPointerException does get reported
at the top level.

Explicitly declaring NullPointerException or any RuntimeException, I
can't even understand what it means. A method can throw it even if it
does not declare it in throws clause and declaring it in throws clause
does not force the callers to catch it.
 
P

Patricia Shanahan

Hemal said:
I can't figure it out. I am sure I am missing something and will
appreciate clarification.

Most often, the callers of a method "throws Exception" themselves in
turn "throws Exception" and the NullPointerException does get reported
at the top level.

Explicitly declaring NullPointerException or any RuntimeException, I
can't even understand what it means. A method can throw it even if it
does not declare it in throws clause and declaring it in throws clause
does not force the callers to catch it.

There is a valid use for declaring a RuntimeException subclass in a
throws clause. It provides a hook for a Javadoc explanation of the
meaning of the exception.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top