使用 ConnectionProvider 和 ExecuteListeners 的 DSL

DSL Using ConnectionProvider and ExecuteListeners

我正在切换我的代码以使用连接提供程序而不是配置。在此过程中,我无法使用自定义执行侦听器来记录我的 SQL 语句。

所以有几个问题:

  1. 如果我使用的是 连接提供者?
  2. 连配置都不需要吗?
  3. 我要在 "release" 方法中输入什么?

        conn = DriverManager.getConnection( url, userName, password );
        conn.setSchema( schema );
    
        configuration = new DefaultConfiguration().set( conn ).set( SQLDialect.MYSQL );
        configuration.set( new DefaultExecuteListenerProvider( new IFSCustomJOOQExecuteListener( true, true ) ) );
    
        aDSL = DSL.using( configuration );
    

我把它切换过来,现在看起来像这样:

    IFSConnectionProvider ifsConnectionProvider = new IFSConnectionProvider();
    aDSL = DSL.using( ifsConnectionProvider, SQLDialect.MYSQL );

这是连接提供商的代码(删除了不相关的行):

    public class IFSConnectionProvider implements ConnectionProvider
        {
        protected Connection conn;
        protected Configuration configuration;

        @Override
        public Connection acquire() throws DataAccessException
            {
            try
                {
                if (conn == null)
                    {
                    conn = DriverManager.getConnection( url, userName, password );
                    conn.setSchema( schema );
                    conn.setAutoCommit( false );

                    configuration = new DefaultConfiguration().set( conn ).set( SQLDialect.MYSQL );
                    configuration.set( new DefaultExecuteListenerProvider( new IFSCustomJOOQExecuteListener( true, true ) ) );
                    }
                return conn;
                }
            catch (SQLException ex)
                {
                ... yada yada yada ...
                }

            }

        @Override
        public void release( Connection connection ) throws DataAccessException
            {
            // TODO Auto-generated method stub
            // do i need to do a release?
            }
        }

这里有几点值得一提:

I am switching my code to use a Connection Provider rather than a configuration

您仍在使用配置。因为在引擎盖下,总是有一个配置,即使它是为您创建的,包装您的连接提供程序。

  1. Is there a way to use a custom listener if I am using a connectionProvider?

这两件事没有关系。无论如何,在某些时候你需要将两者都放在你的配置中

  1. Is the configuration not even needed?

是的,需要。问题是您是否需要自己创建一个。如果您只是将连接提供程序传递给 DSL.using()(这只是方便 API!它会为您创建配置),则不需要这样做。

但是如果您想使用执行侦听器,则需要自己手动创建一个配置。

  1. What do I put in the "release" method?

在你的例子中,你永远不会在释放方法中做任何事情,因为你自己创建了一个独立的连接。创建连接的逻辑应该关闭它,而不是 jOOQ(即不是 "release")

public class IFSConnectionProvider implements ConnectionProvider
        {
        protected Connection conn;
        protected Configuration configuration;

恐怕这不会有任何进展,原因有二:

  1. 您可能不应该在连接提供程序中缓存连接。我不明白为什么这会有用。
  2. 连接提供程序是您放入配置中的内容。反其道而行之是没有意义的。

我觉得整个问题都是对jOOQ各种SPI的误解造成的:

  1. 总有一个配置
  2. 如果您不自己创建,jOOQ 的便捷方法会为您创建
  3. 配置包含 ConnectionProvider,反之亦然。