[英]JOOQ initializing DAO Best Approach
I want to know Best practices for initilizing JOOQ generated DAO. 我想知道初始化JOOQ生成的DAO的最佳实践。 Now,I am using following approach for initilization of JOOQ generated DAO.
现在,我正在使用以下方法来启动JOOQ生成的DAO。 In following case StudentDao is JOOQ generated.
在下面的例子中,StudentDao是JOOQ生成的。
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;
}
}
}
and I have doubt with using above DAO my example code is below. 我怀疑使用上面的DAO我的示例代码如下。
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: 有两种方法可以看到这种初始化:
Your approach is correct, but might be considered a bit heavy. 你的方法是正确的,但可能会被认为有点沉重。 You're creating a new
DAO
every time you need it. 您每次需要时都会创建一个新的
DAO
。
As of jOOQ 3.7, a DAO
is a pretty lightweight object. 从jOOQ 3.7开始,
DAO
是一个非常轻量级的对象。 The same is true for the Configuration
that wraps your Connection
. 包装
Connection
的Configuration
也是如此。
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. 一旦您的项目发展(或在未来的jOOQ版本中),这可能不再适用,因为您的
Configuration
初始化(或jOOQ的DAO
初始化)可能会变得更重。
But this is a small risk, and it would be easy to fix: 但这是一个小风险,并且很容易修复:
DAO
or Configuration
references DAO
或Configuration
引用 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. 大多数人只为他们的应用程序设置一个jOOQ
Configuration
,并且在服务中的某个地方只设置一个DAO
实例(每个DAO
类型)。 In this case, your Configuration
must not share the Connection
reference, but provide a Connection
to jOOQ via the ConnectionProvider
SPI . 在这种情况下,您的
Configuration
必须不共享Connection
的参考,但提供了一个Connection
通过到jOOQ 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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.