开发者

Configuring DAO factory with Pooled DataSource

开发者 https://www.devze.com 2023-02-02 17:17 出处:网络
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 b

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 t开发者_JS百科he application server e.g. Glassfish

When the factory is created for the first time (Singleton pattern) it does a JNDI lookup for the pooled datasource e.g. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号