简体   繁体   中英

Configuring DAO factory with Pooled DataSource

I'm after a bit of advice regarding configuring a DAO factory with a pooled datasource. Suppose its a JDBC DAO factory (from an abstract factory) and the pooled datasource is configured and managed by the application server eg Glassfish

When the factory is created for the first time (Singleton pattern) it does a JNDI lookup for the pooled datasource eg from a properties file, which will set the pooled datasource on the JDBC DAO factory.

Then, when you instantiate and return the concrete DAO would you pass it a reference to datasource so it could retrieve a connection to the database?

Basically what I did was encapsulate that datasource as a field in a base class called DAO. In the constructor of the DAO you pass in the JNDI name of the connection you want.

public DAO(String jndiName) throws NamingException {
  ds = DataSourceFactory.getInstance().lookup(jndiName);
}

Then in all of your concrete classes you simply extend from DAO and can use the datasource as you want.

public concreteDAO() throws NamingException {
  super("Some JNDI Name That this DAO should know");
}

The same DAO class has some other utility methods like a cleanup method, that silently closes the ResultSet, Statements and Connections. So that way I just have to add this in the finally clause of all my methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM