简体   繁体   English

Java动态事务代理-连接初始化

[英]Java Dynamic Transaction Proxy - connection initialization

I have such things as: 我有这样的事情:
1) DAO class with methods, used to perform transaction, such as withdrawSum(int idAccount, float amount) and putSum(int idAccount, float amount) which use java.sql.Connection and java.sql.PreparedStatement classes to perform atomic operations with DB. 1)具有用于执行事务的方法的DAO类 ,例如withdrawSum(int idAccount, float amount)putSum(int idAccount, float amount) ,它们使用java.sql.Connection和java.sql.PreparedStatement类来执行原子操作D B。
2) java.lang.reflect. 2)java.lang.reflect。 InvocationHandler implementor, whick is used to get connection before transaction and commit/rollback after transaction: InvocationHandler实现者,whick用于在事务之前获取连接,并在事务之后进行提交/回滚:

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Connection connection = null;
    try{
        connection = DaoUtil.INSTANCE.getConnection();
        connection.setAutoCommit(false);
        method.invoke(connection, args);
        connection.commit();
    } catch(InvocationTargetException ex){
        connection.rollback();
    } finally{
        DaoUtil.INSTANCE.closeConnection(connection);
    }
    return null;
}

3) Transaction manager , which creates Proxy instance and with its help calls method which executes transaction, something like this: 3) 事务管理器 ,它创建Proxy实例,并使用其帮助调用方法执行事务,如下所示:

TransactionManager transManager = new TransactionManager();
InvocationHandler transHandler = new MyInvocationHandler(transManager);
TransactionManager proxy = (TransactionManager) Proxy.newProxyInstance(
            transManager.getClass().getClassLoader(), transManager.getClass().getInterfaces(), transHandler);
proxy.transferMoney(withdrawAccountid, putAccountId, transactionSum);

..... .....

   public void transferMoney(int withdrawAccountid, int putAccountId, float transactionSum){
            AccountDao.getInstance().withdrawSum(withdrawAccountid, transactionSum);
            AccountDao.getInstance().putSum(putAccountId, transactionSum);
    }

The question is : to execute statement in DAO methods, I need initialized Connection object. 问题是 :要在DAO方法中执行语句,我需要初始化Connection对象。 It is intialized and passed to the invoke -method of the InvocationHandler. 它被初始化并传递给InvocationHandler的invoke方法。 How it should be correctly initialized in DAO-methods? 如何在DAO方法中正确初始化它? Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

Since transactions are naturally associated with threads, a typical approach here is to store Connection in a ThreadLocal storage during the scope of transaction. 由于事务自然与线程相关联,因此此处的典型方法是在事务范围内将Connection存储在ThreadLocal存储中。

Then you can make these connections available to DAOs using different approaches: 然后,您可以使用不同的方法将这些连接提供给DAO:

  • DAOs can obtain Connection s by calling some static method DAO可以通过调用一些静态方法来获得Connection

  • A custom DataSource can be injected into DAOs - its getConnection() method would return the connection associated with current transaction, note that connection should be proxied in order to ignore close() . 可以将自定义DataSource注入DAO中-它的getConnection()方法将返回与当前事务关联的连接,请注意,应该使用该代理作为代理,以忽略close() This approach doesn't couple your DAOs with transaction management code. 这种方法不会将您的DAO与事务管理代码结合在一起。

Also note that all these things are already implemented by some libraries, for example, by Spring Framework. 还要注意,所有这些东西已经由某些库(例如,Spring Framework)实现。 Perhaps you can leverage it instead of creating your own solution, or at least take a look at their design (in Spring, different approaches for obtaining connections are implemented by DataSourceUtils and TransactionAwareDataSourceProxy , respectively). 也许您可以利用它代替创建自己的解决方案,或者至少看看它们的设计(在Spring中,分别通过DataSourceUtilsTransactionAwareDataSourceProxy实现获取连接的不同方法)。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM