简体   繁体   English

使用合并的数据源配置DAO工厂

[英]Configuring DAO factory with Pooled DataSource

I'm after a bit of advice regarding configuring a DAO factory with a pooled datasource. 我对使用池化数据源配置DAO工厂有一些建议。 Suppose its a JDBC DAO factory (from an abstract factory) and the pooled datasource is configured and managed by the application server eg Glassfish 假设它是一个JDBC DAO工厂(来自抽象工厂),并且池化的数据源是由应用服务器(例如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. 首次创建工厂(Singleton模式)时,它将对合并的数据源进行JNDI查找,例如从属性文件中查找,这将在JDBC DAO工厂上设置合并的数据源。

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? 然后,当实例化并返回具体的DAO时,是否将其传递给数据源引用,以便它可以检索与数据库的连接?

Basically what I did was encapsulate that datasource as a field in a base class called DAO. 基本上,我所做的就是将该数据源封装为一个称为DAO的基类中的字段。 In the constructor of the DAO you pass in the JNDI name of the connection you want. 在DAO的构造函数中,您传入所需连接的JNDI名称。

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. 然后,在所有具体的类中,您都可以简单地从DAO进行扩展,并可以根据需要使用数据源。

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. 相同的DAO类还具有其他一些实用程序方法,例如清理方法,该方法可以静默关闭ResultSet,语句和连接。 So that way I just have to add this in the finally clause of all my methods. 这样,我只需要在所有方法的finally子句中添加此内容即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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