【发布时间】:2012-01-03 09:31:23
【问题描述】:
我有一些 c3p0 池封装在一个用于执行 SQL 语句的类中。 它是这样初始化的:
public PooledQueryExecutor(String url, Properties connectionProperties) throws DALException {
try {
dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(DRIVER);
dataSource.setJdbcUrl(url);
dataSource.setProperties(connectionProperties);
} catch (PropertyVetoException ve) {
throw new DALException(ve);
}
}
然后 - 在同一个类中 - 我使用一些方法来执行基本任务:
public CachedRowSet executeSelect(String sql) throws DALException {
// Get a connection, execute the SQL, return the rows that match, return resources to the pool
}
“问题”是:
我有很多不同的类来代表我收到的网络数据包。大多数类都需要这个 PooledQueryExecutor 来执行数据库操作,但有些则不需要。我是将这个 PooledQueryExecutor 传递给需要它的类的构造函数(80% 的数据包),还是让 PooledQueryExecutor 成为单例?或者也许是“别的东西”?我也想过使用ThreadLocal 来避免污染我的构造函数,但我认为这不是一个好主意,是吗?
编辑:它不是一个网络应用程序,目前没有使用依赖注入框架。
感谢您的宝贵时间!
【问题讨论】:
标签: java jdbc connection-pooling