我正在重构一个使用 hibernate 和 dao 模式的 jsf Web 应用程序 dal,目前每个数据访问类将自己的 session 和事务处理到每个方法中(每个操作一个 session 是反模式吗?) 例如:
public void saveEntity(ModelEntity entity) throws Exception {
String entityName = getCleanClassName(entity.getClass());
Session session = null;
Transaction tx = null;
try {
session = SessionFactoryUtil.getInstance().openSession();
tx = session.beginTransaction();
session.saveOrUpdate(entity);
session.flush();
tx.commit();
} catch (Exception ex) {
tx.rollback();
...
throw new Exception(msg, ex);
} finally {
session.close();
}
}
以上类可以直接用于支持 bean 操作或通过某些业务逻辑类方法(例如创建新的分离对象、设置一些值并通过 DAO 对象存储它们)。现在我发现一些业务逻辑类方法需要原子执行,例如:
//inside some backing bean actionlistener
DocumentoManager dm = new DocumentoManager();
PraticaManager pm = new PraticaManager();
Documento newDoc = new Documento();
newDoc.setDataArrivo(new java.util.Date newDoc.setNote("...");
Allegato newAll = new Allegato();
newAll.setTitolo(newDoc.getNote());
//businness logic methods that should be executed atomically
dm.creaDocumentoDaAllegato(newDoc,newAll,
event.getFile().getContents(),
event.getFile().getFileName(),
"");
pm.collegaDocumento(pratica, newDoc);
最后我有一个 sessionFactoryUtil 类来管理 hibernate session (getcurrentSession、openSession 等...)。
如何重构上述架构?我正在考虑执行以下操作:
- 从 DAO 对象中删除交易:我不能放 事务管理到 bl 类(xxxManager)中,所以我应该 使用一个只创建、提交或回滚事务的类, 封装sessionFactoryUtil,并在后台使用 beans(比如 jta userTransaction)?
- 如果有人尝试直接使用 DAO 对象,我该如何设置 session ?将 sessionFactoryUtil 访问到 DAO 方法中?
- 在 DAO 内调用 getCurrentSession 安全吗?
- 如果我创建一个类来处理业务逻辑事务,我如何确定它是线程安全的?
我认为 sessionFactoryUtil 是线程安全的,因为它是按照 hibernate 指南建议的模式编写的:
private SessionFactoryUtil() {
}
static {
// Annotation and XML
// sessionFactory = new
// AnnotationConfiguration().configure().buildSessionFactory();
// XML only
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
...
}
}
public static SessionFactory getInstance() {
try{
return sessionFactory;
} catch (Exception ex)
{
return null;
}
}
最佳答案
交易不应成为 DAO 的一部分。这些应该由管理 DAO、模型对象和事务的服务层来处理,以满足用例。服务层拥有并管理工作单元。
DAO 应该只涉及持久性。他们不需要了解有关 session 的任何信息。让服务为 DAO 提供持久化对象所需的内容。
关于java - 使用 hibernate、dao、服务和工作单元重构 dal : how to put all togheter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7819170/