什么是可用于Java/JDBC的最佳连接池库?
我正在考虑2个主要候选人(免费/开源):
我在博客和其他论坛上阅读了很多关于它们的内容,但无法做出决定.
这两个是否有任何相关的替代方案?
我应该将哪个JDBC连接池库用于新的应用程序项目(而不是Web应用程序)?
我应该选择BoneCP吗?我没有任何巨大的要求.我只需要一个好的,易于使用的数据库连接池,即正在开发中.图书馆,作者可以回复错误报告,回答一些具体问题等.
顺便说一下,实际上,我只使用MySQL.我发现,MySQL驱动程序支持DriverManager接口,但我不确定它是否实际上是连接池.
我将Hibernate与MySQL 5.1.30一起使用.
我有下一个库:
我使用hibernate.cfg.xml进行配置:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/fooDatatbase</property>
<property name="connection.username">foo</property>
<property name="connection.password">foo123</property>
<!-- Use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_periods">3000</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout --> …
Run Code Online (Sandbox Code Playgroud) 我有一个基本的Spring JDBC应用程序,它具有非常基本的配置:
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@1.1.1.1:1521:XXX"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>
<bean id="dbThing" class="com.DbThing">
<property name="dataSource" ref="myDataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
我想介绍一个连接池,并在阅读了几个线程后,我对使用哪个池库感到有点困惑.
似乎在SO上有更多学分的图书馆是CP30和DBCP.由于我使用的是Oracle,我还可以使用驱动程序提供的池数据源.我知道有更多的库可用 - 例如新的Apache Tomcat 7池库.
有没有我应该避免的图书馆?
我应该在给定的库中使用任何推荐的配置吗?
你想分享的任何"战争故事"?
我正在努力面对c3p0配置面临的问题.我上周发布了一个问题 - C3P0配置!在哪里和如何?,但没有收到答复.任何帮助表示赞赏.
嘿所有,我刚刚开始使用c3p0进行数据库连接池.它当前附加到我的log4j输出.如何为c3p0设置注销或至少为SEVERE级别?我尝试调整属性文件但不确定它是否被正确拾取.
关于如何最好地关闭它的任何想法?
谢谢
更新:这似乎在log4j.properties文件中工作
log4j.logger.com.mchange.v2.c3p0.impl=INFO
log4j.logger.com.mchange=INFO
Run Code Online (Sandbox Code Playgroud) 以下是我的帮助类获取数据库连接:
我已经使用了这里描述的C3P0连接池.
public class DBConnection {
private static DataSource dataSource;
private static final String DRIVER_NAME;
private static final String URL;
private static final String UNAME;
private static final String PWD;
static {
final ResourceBundle config = ResourceBundle
.getBundle("props.database");
DRIVER_NAME = config.getString("driverName");
URL = config.getString("url");
UNAME = config.getString("uname");
PWD = config.getString("pwd");
dataSource = setupDataSource();
}
public static Connection getOracleConnection() throws SQLException {
return dataSource.getConnection();
}
private static DataSource setupDataSource() {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(DRIVER_NAME);
} …
Run Code Online (Sandbox Code Playgroud) 我在Tomcat中使用C3P0作为连接池,我看到非常令人担忧的错误:
2010-09-16 13:25:00,160 [Timer-0] WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@43502400 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
2010-09-16 13:25:01,407 [Timer-0] WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@43502400 -- APPARENT DEADLOCK!!! Complete Status:
Managed Threads: 10
Active Threads: 0
Active Tasks:
Pending Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@6e4151a7
Pool thread stack traces:
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#6,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#5,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#4,5,main]
java.lang.Object.wait(Native Method)
... many more, exact same stack trace
Run Code Online (Sandbox Code Playgroud)
第534行是:
while (true) {
Runnable …
Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个多租户Web应用程序,同时具有(理想情况下)数据库分离和模式分离方法的可能性.虽然我将从Schema分离开始.我们目前正在使用:
大部分时间我都遵循这个主题(因为解决方案@Transactional
).但我在执行方面有点迷失MultiTenantContextConnectionProvider
.在这里也提到了类似的问题,但有些方面我无法弄清楚:
1)连接池会发生什么?我的意思是,它是由Spring还是Hibernate管理的?我猜ConnectionProviderBuilder
- 或者按照建议 - 任何实现,Hibernate都是管理它的人.
2)Spring不管理连接池是一种好方法吗?或者Spring是否可以管理它?
3)这是未来实现数据库和模式分离的正确途径吗?
任何评论或描述都完全赞赏.
应用程序的context.xml
<beans>
...
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="c3p0DataSource" />
</bean>
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
... other C3P0 related config
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="com.webapp.domain.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.default_schema">public</prop>
<prop key="hibernate.multiTenancy">SCHEMA</prop>
<prop key="hibernate.tenant_identifier_resolver">com.webapp.persistence.utility.CurrentTenantContextIdentifierResolver</prop>
<prop key="hibernate.multi_tenant_connection_provider">com.webapp.persistence.utility.MultiTenantContextConnectionProvider</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="autodetectDataSource" …
Run Code Online (Sandbox Code Playgroud) 我有一个模型有一个相当大的子实体图和hibernate最终制作了大约9个语句懒洋洋地获取所需的所有数据但大约4级深度我得到一个"无法初始化代理 - 没有会话"错误,我是不知道为什么.
调节器
@Transactional(readOnly = true)
@RequestMapping(value = "/v2/plans", method = RequestMethod.GET)
public @ResponseBody List<PlanPresenter> show(HttpServletRequest request) throws Exception {
List<PlanPresenter> planPresenters = new ArrayList<>();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Plan> planQuery = criteriaBuilder.createQuery(Plan.class);
Root<Plan> root = planQuery.from(Plan.class);
if (request.getParameter("region") != null || request.getParameter("group") != null) {
List<Predicate> criteria = new ArrayList<Predicate>();
if (request.getParameter("region") != null) {
criteria.add(criteriaBuilder.equal(root.get(Plan_.region), request.getParameter("region")));
}
if (request.getParameter("group") != null) {
criteria.add(criteriaBuilder.equal(root.get(Plan_.groupCode), request.getParameter("group")));
criteria.add(root.get(Plan_.planSetId).in(groupPlanSetIds));
} else {
criteria.add(root.get(Plan_.planSetId).in(currentPlanSetIds));
}
Query query = entityManager.createQuery(planQuery.where(criteriaBuilder.and(criteria.toArray(new Predicate[]{}))));
for (Plan …
Run Code Online (Sandbox Code Playgroud) c3p0 ×10
java ×7
hibernate ×4
jdbc ×2
spring ×2
bonecp ×1
database ×1
jpa-2.0 ×1
multi-tenant ×1
postgresql ×1
spring-boot ×1