JOOQ初始化DAO最佳方法 [英] JOOQ initializing DAO Best Approach

查看:186
本文介绍了JOOQ初始化DAO最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解初始化JOOQ生成的DAO的最佳实践.现在,我正在使用以下方法初始化JOOQ生成的DAO.在以下情况下,StudentDao是JOOQ生成的.

I want to know Best practices for initilizing JOOQ generated DAO. Now,I am using following approach for initilization of JOOQ generated DAO. In following case StudentDao is JOOQ generated.

public class ExtendedStudentDAO extends StudentDao {
    public ExtendedStudentDAO () {
        super();
    }

    public ExtendedStudentDAO (Connection connection) {
        Configuration configuration = DSL.using(connection,
                JDBCUtils.dialect(connection)).configuration();

        this.setConfiguration(configuration);
    }

    //adding extra methods to DAO using DSL
    public String getStudentName(Long ID)
            throws SQLException {

        try (Connection connection = ServiceConnectionManager.getConnection()) {

            DSLContext dslContext = ServiceConnectionManager
                    .getDSLContext(connection);

            Record1<String> record = dslContext
                    .select(Student.Name)
                    .from(Student.Student)
                    .where(Student.ID
                            .equal(ID)).fetchOne();

            if (record != null) {
                return record.getValue(Student.Name);
            }

            return null;
        }
    }
}

我对使用上面的DAO感到怀疑,我的示例代码如下.

and I have doubt with using above DAO my example code is below.

try (Connection connection = ServiceConnectionManager.getConnection()) {

ExtendedStudentDAO extendedStudentDAO =new ExtendedStudentDAO(connection);

Student stud=new Student();
.....
....

//insert method is from Generated DAO
extendedStudentDAO.insert(stud); 

//this method is added in extended class
extendedStudentDAO.getStudentName(12);

}

推荐答案

查看这种初始化有两种方法:

There are two ways to look at this kind of initialisation:

您的方法是正确的,但可能会被认为有点繁重.每次需要时,您都在创建一个新的DAO.

Your approach is correct, but might be considered a bit heavy. You're creating a new DAO every time you need it.

从jOOQ 3.7开始,DAO是一个非常轻巧的对象.包裹ConnectionConfiguration也是一样.

As of jOOQ 3.7, a DAO is a pretty lightweight object. The same is true for the Configuration that wraps your Connection.

一旦您的项目进行了开发(或在将来的jOOQ版本中),由于您的Configuration初始化(或jOOQ的DAO初始化)可能会变得越来越重,这可能不再成立.

Once your project evolves (or in future jOOQ versions), that might no longer be true, as your Configuration initialisation (or jOOQ's DAO initialisation) might become heavier.

但这是一个很小的风险,而且很容易解决:

But this is a small risk, and it would be easy to fix:

大多数人将为他们的应用程序只设置一个jOOQ Configuration,并且在服务中的某个位置也只设置一个DAO实例(每种DAO类型).在这种情况下,您的Configuration不得共享Connection引用,而应通过

Most people will set up only a single jOOQ Configuration for their application, and also only a single DAO instance (per DAO type), somewhere in a service. In this case, your Configuration must not share the Connection reference, but provide a Connection to jOOQ via the ConnectionProvider SPI. In your case, that seems trivial enough:

class MyConnectionProvider implements ConnectionProvider {
    @Override
    public Connection acquire() {
         return ServiceConnectionManager.getConnection();
    }

    @Override
    public void release(Connection connection) {
         try {
             connection.close();
         }
         catch (SQLException e) {
             throw new DataAccessException("Error while closing", e);
         }
    }
}

这篇关于JOOQ初始化DAO最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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