如何在dao中使用jndi数据源? [英] How to use jndi datasource in dao?

查看:156
本文介绍了如何在dao中使用jndi数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tomcat,jsp,servlet和log4j做我的第一个Web项目。我有 TO ,例如:User,Subject,Faculty等,以及 DAO 对象,例如:UserRepository,SubjectRepository,FacultyRepository等。对于存储库,我具有以下层次结构(并非放置了所有实体):

AbstractRepository 中的 DataSource 的初始化是这样的:

I'm trying to do my first web project using tomcat, jsp, servlets and log4j. I have TO like: User, Subject, Faculty etc, and DAO objects like: UserRepository, SubjectRepository, FacultyRepository etc. With repositories I have the following hierarchy (not all entities placed): The initialization of DataSource in AbstractRepository goes this way:

public abstract class AbstractRepository<T> implements Repository<T> {

private final static Logger LOG = Logger
        .getLogger(AbstractRepository.class);
private static final DataSource ds = init();

private static  DataSource init() {
    DataSource dataSource = null;
    try {
        Context initContext = new InitialContext();
        dataSource = (DataSource) initContext
                .lookup("java:/comp/env/jdbc/mydb");
    } catch (NamingException ex) {
        LOG.error("Cannot obtain a connection from the pool", ex);
    }
    return dataSource;
}
protected Connection getConnection() throws SQLException {
    return ds.getConnection();
}
....
}

现在,如果存储库需要一个 Connection ,它只需调用父 getConnection()方法。

So now if repository needs a Connection its just calls the parent getConnection() method.

问题是最好在 AbstractRepository 中有一个 DataSource 对象,每个子类存储库都将得到 Connection 在父类或每个子类中的使用方法应该具有自己的 private final DataSource 字段,该字段将在构造函数中初始化吗?然后向我解释:如果我选择第一种方法,应该使用getConnection synchronized 关键字方法吗?而且,如果我选择第二种方式:那么子类存储库应该是单调的,因为每次请求到达servlet时,它都会创建一些存储库,因此它将是多个 DataSources ?或者 Tomcat 通过 context.xml 文件知道应该保留多少个连接?我只是感到困惑。您能解释一下最佳做法吗?也许我应该重新设计一些东西?

The question is it better to have one DataSource object in AbstractRepository and each subclass repository will get Connection using method in parent or each subclass should have its own private final DataSource field which would be initialized in constructor ? Then explain me: if I choose first way should I put on method getConnection synchronized keyword ? And if I choose second way: then subclass repositories should be singletones, because every time the request comes to a servlet it creates some repository and so it will be multiple DataSources? Or Tomcat knows through context.xml file how many Connections it should keep ? I've just got confused. Would you explain the best practices ? Maybe I should re-design something ?

推荐答案

我以前遇到过很多次。我会有一个名为CommonDao的类,它与您的AbstractRepository类似。 Connection或DataSource是属于CommonDao的变量,但它不是静态的……因此CommonDao的每个实例都有自己的副本。所以我的答案是,只要您不将AbstractRepository.ds设置为静态,就可以了。

I have come across this many times before. I would have a class called CommonDao that is similar to your AbstractRepository. The Connection or DataSource was a variable that belonged to CommonDao, but it was not static... so every instance of CommonDao had their own copy. So my answer is that as long as you do not make AbstractRepository.ds static then you should be fine.

这样做的优点(让DataSource成为AbstractRepositor的一部分,但不是静态的)是您有一种获取DataSource的常用方法,并且可以在需要时将其覆盖子类(这需要使ds保护)。

The pros for doing this (having DataSource be part of AbstractRepositor but not be static) is that you have one common way for obtaining your DataSource and can overwrite it if need be by the subclasses (this would require making ds protected).

这篇关于如何在dao中使用jndi数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆