【发布时间】: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