使用池化数据源配置 DAO 工厂

发布于 2024-10-11 02:36:58 字数 232 浏览 3 评论 0原文

我正在寻求一些有关使用池数据源配置 DAO 工厂的建议。假设它是一个 JDBC DAO 工厂(来自抽象工厂),并且池数据源由应用程序服务器(例如 Glassfish)配置和管理。

当第一次创建工厂(单例模式)时,它会对池数据源进行 JNDI 查找,例如一个属性文件,它将在 JDBC DAO 工厂上设置池数据源。

然后,当您实例化并返回具体的 DAO 时,您是否会向其传递对数据源的引用,以便它可以检索到数据库的连接?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2024-10-18 02:36:58

基本上我所做的就是将该数据源封装为名为 DAO 的基类中的字段。在 DAO 的构造函数中,您传入所需连接的 JNDI 名称。

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

然后,在所有具体类中,您只需从 DAO 扩展并可以根据需要使用数据源。

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

同一个 DAO 类还有一些其他实用方法,例如清理方法,它会默默地关闭结果集、语句和连接。这样我只需将其添加到所有方法的finally 子句中即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文