连接池.每次执行操作时调用一个过程 [英] Connection pool. call a procedure each time an operation is made

查看:88
本文介绍了连接池.每次执行操作时调用一个过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在那儿,我正在开发Primefaces 5/JSF 2/Mybatis网络应用程序.我的问题是.要知道每次(在应用程序上)谁做了什么,我们必须执行setUser(...)方法.我现在正在使用的公司拥有我们正在构建的应用程序的C#版本,但是那里没有连接池,因此他们只需要在用户登录时执行该过程即可.

Hei there, I'm working on a Primefaces 5/JSF 2/Mybatis webapp. My question is. To know each time who did what (on the app) we have to execute a method setUser(...). The company I'm working in right now, had a C# version of the app we are building now but there were no connection pools there so they only had to execute that procedure when the user logged in.

(现在,我们只是在getSQLFactory方法中调用该方法,我知道这不是最佳实践……但是,这似乎是唯一可行的方法,无需在我们的所有200多个Mappers中手动添加它)

(right now we just call that method in the getSQLFactory method, which I know is not best practice... but that looked like the only viable solution to not add it manually in all our 200+ Mappers)

public static SqlSessionFactory getSqlSessionFactory() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
ManagedNavUtils navyUtils = (ManagedNavUtils) session.getAttribute("navy");
if (navyUtils != null && navyUtils.getLoggedInUser() != null)
  setLoggedInUser(navyUtils.getLoggedInUser());
return factory;

}

是否有一种方法可以在每次在DB上执行某些操作时调用该过程?

IS there a way to call the procedure each time something gets executed on the DB?

具有相关配置的我的mybatis-config.xml文件:

my mybatis-config.xml file with the relevant configuration:

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="${database.driver}" />
            <property name="url" value="${database.url}" />
            <property name="username" value="${database.username}" />
            <property name="password" value="${database.password}" />

            <!-- CONNECTION POOLING PROPERTIES -->
            <property name="poolMaximumActiveConnections" value="20" />
            <property name="poolMaximumIdleConnections" value="5" />
            <property name="poolPingEnabled" value="false" />
        </dataSource>
    </environment>
</environments>

推荐答案

您可以创建mybatis

You can create mybatis plugin to intercept all queries.

但是对于您而言,更好的选择是在连接检索期间仅将用户设置一次.从连接池借用连接时,某些连接池允许自定义初始化.另一种可能的选择是包装mybatis使用的DataSource.

But for your case better option is to set user only once that is during connection retrieval. Some connection pools allows custom initialization when connection is borrowed from the pool. Another possible option is to wrap DataSource which is used by mybatis.

对于使用mybat附带的数据源的情况,这是使用自定义DataSourceFactory包装DataSource的最简单方法.

For your case that is using datasource shipped with mybatis the easiest way it to wrap DataSource using custom DataSourceFactory.

首先像这样扩展PooledDataSource:

class MyPooledDataSource extends PooledDataSource {

   // all constructors should be defined but omitted for brevity

    public Connection getConnection() throws SQLException {
        Connection connection = super.getConnection();
        setLoggedInUser(connection);
        return connection;
    }

    public Connection getConnection(String username, String password) throws SQLException {
        Connection connection = super.getConnection(username, password);
        setLoggedInUser(connection);
        return connection;
    }

    private void setLoggedInUser(Connection connection) {
        // actual setting of logged in user
    }
}

然后您需要定义工厂:

public class MyDataSourceFactory extends PooledDataSourceFactory {

    public MyDataSourceFactory() {
        this.dataSource = new MyPooledDataSource();
    }
}

并修改mybatis-config.xml以使mybatis使用您的数据源工厂:

And modify mybatis-config.xml to make mybatis use your datasource factory:

<environments default="development">
  <environment id="development">
    <transactionManager type="JDBC" />
    <dataSource type="org.myproject.MyDataSourceFactory">
        <property name="driver" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />

        <!-- CONNECTION POOLING PROPERTIES -->
        <property name="poolMaximumActiveConnections" value="20" />
        <property name="poolMaximumIdleConnections" value="5" />
        <property name="poolPingEnabled" value="false" />
    </dataSource>
  </environment>
</environments>

这篇关于连接池.每次执行操作时调用一个过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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