DAO设计模式和连接处理 [英] DAO Design Pattern and Connection Handling

查看:138
本文介绍了DAO设计模式和连接处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有EmployeeDAOImpl.java和DepartmentDAOImpl.java。现在我从数据服务层调用这两个。 EmployeeDAO和DepartmentDAO是接口,它们只能减少CRUD方法。在服务层我有一个方法调用createEmployee(Employee employee)和createDepartment(Department department)。这两个呼叫应该在同一个事务中,这意味着只有在2个呼叫成功后才能关闭连接。现在你们有什么想法如何提供连接。我不想在DepartmentDAOImpl和EmployeeDAOImpl的构造函数提供连接。我也不想使用Spring AOP或ThreadLocale,有没有解决方法呢?

解决方案

您不解释为什么不想提供与DAO构造函数的连接或使用Spring AOP或ThreadLocale



我会说连接获取是事务管理的一部分,这是服务层的责任。它应该提供DAO与他们需要连接到数据库。



你说过你不会这样做,但没有提供关于你将如何



我会说服务应该从池中获取连接,将其发送给DAO,管理事务,并将连接返回到池当用例完成时。



我将向您介绍实施细节,因为您不想使用Spring。使用直接的JDBC可以做到这一点。您只需要努力工作。



如果您坚持不使用Spring或AOP,您的服务代码将如下所示:

 包服务; 

public class FooServiceImpl实现FooService {
public void saveFoo(Foo f){
Connection connection = null;
语句st = null;
try {
connection = ConnectionPool.checkout();
connection.setAutoCommit(false);
//在这里做某事
connection.commit();
} catch(Exception e){
rollback(connection);
e.printStackTrace();
} finally {
close(st);
关闭(连接);
}
}
}

Spring和AOP将成为在这里受益。它将消除样板代码。这是你的选择。


I have EmployeeDAOImpl.java and DepartmentDAOImpl.java. Now I am calling this two from data service layer. EmployeeDAO and DepartmentDAO is interface and they have only abtract CRUD methods. In Service layer I have two method call createEmployee(Employee employee) and createDepartment(Department department) with in a method. These 2 calls should be in same transaction that means you cant close connection only after 2 calls succeeded. Now do you people have any idea how to supply a connection. I dont want to supply a connection at the constructor of DepartmentDAOImpl and EmployeeDAOImpl. Also I dont want to use Spring AOP or ThreadLocale, Is there any solution to do that?

解决方案

You don't explain why you don't want to supply a connection to the DAO constructors or use Spring AOP or ThreadLocale.

I would say that the connection acquisition is part of the transaction management, which is the responsibility of the service tier. It should supply the DAOs with what they need to connect to the database.

You've said how you won't do it, but offered no ideas about how you would do it.

I'd say that service ought to get the connection from a pool, give it to the DAOs, manage the transaction, and return the connection to the pool when the use case is done.

I'll leave implementation details to you, since you don't want to use Spring. It's possible to do it using straight JDBC. You'll just have to work harder to do it.

If you insist on not using Spring or AOP, your service code will look something like this:

package service;

public class FooServiceImpl implements FooService {
    public void saveFoo(Foo f) {
        Connection connection = null;
        Statement st = null;
        try {
            connection = ConnectionPool.checkout();
            connection.setAutoCommit(false);
            // do something here
            connection.commit();
        } catch (Exception e) {
            rollback(connection);
            e.printStackTrace();
        } finally {
            close(st);
            close(connection);
        }
    }
}

Spring and AOP will be a benefit here. It'll eliminate the boilerplate code. It's your choice.

这篇关于DAO设计模式和连接处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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