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;
}
}
//////////////////////////////////////
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;
}
}
//////////////////////////////////////