【问题标题】:Creating JDBC transaction for second data source with existing connection pool in Glassfish在 Glassfish 中使用现有连接池为第二个数据源创建 JDBC 事务
【发布时间】:2012-08-03 09:45:53
【问题描述】:

我有用于处理数据库的主要数据源,它使用一些连接池。现在我想创建第二个数据源,它将使用相同的连接池在 单独的事务中执行日志记录操作,然后是主数据库事务! 据我从 Glassfish docs 了解,如果多个数据源使用同一个连接池,然后它们共享一个事务,直到连接没有关闭(我可能错了,请纠正我)。

那么,有没有办法在连接到数据源时启动新事务?通过设置TransactionIsolation 可以吗?

通过以下方式检索连接:

private synchronized Connection getConnection() {
        if (connection == null) {
            try {
                final Context ctx = new InitialContext();
                final DataSource ds = (DataSource) ctx.lookup(getDataSourceLookupAddress());
                connection = ds.getConnection();
            } catch (final NamingException e) {
                errorHandler.error("Datasource JNDI lookup failed: " + dataSourceLookupAddress + "!");
                errorHandler.error(e.toString());
            } catch (final SQLException e) {
                errorHandler.error("Sql connection failed to " + dataSourceLookupAddress + "!");
                errorHandler.error(e.toString());
            }
        }
        return connection;
    }

【问题讨论】:

    标签: java jdbc transactions glassfish connection


    【解决方案1】:

    我想通了。需要启动一个新线程,然后将创建一个新事务。另见Writing to database log in the middle of rollback

    【讨论】:

      猜你喜欢
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多