Spring 和编程式事务管理的连接池问题
我需要你的帮助来解决 Spring 的连接池问题。 我将 Spring 与 Java 1.4 一起使用(无注释)。
这是数据源和连接池定义:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${database.jdbcName}</value>
</property>
<property name="url" value="jdbc:sybase:${database.url}">
</property>
<property name="username">
<value>${database.username}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
<property name="maxActive">
<value>${database.maxActive}</value>
</property>
<property name="maxIdle" >
<value>${database.maxIdle}</value>
</property>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SybaseDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.dbcp.maxWait">100000</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
</props>
</property>
...
当所有连接都处于活动状态时,如果系统需要新连接,我会收到一条消息“正在打开 JDBC 连接”,并且所有进程都会停止。我不明白为什么所有线程都被锁定。
所有服务和 DAO 类都在 Spring 应用程序上下文中定义为单例。
有什么想法吗?
感谢您的帮助。
贝朗热
I need your help in order to solve connection pool problem with Spring.
I’m using Spring with Java 1.4 (no annotation).
Here is the datasource and the connection pool definition:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${database.jdbcName}</value>
</property>
<property name="url" value="jdbc:sybase:${database.url}">
</property>
<property name="username">
<value>${database.username}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
<property name="maxActive">
<value>${database.maxActive}</value>
</property>
<property name="maxIdle" >
<value>${database.maxIdle}</value>
</property>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SybaseDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.dbcp.maxWait">100000</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
</props>
</property>
...
When all the connections are active, if the system need a new connection I got a message saying “opening JDBC connection” and all the process are stopped. I don’t understand why all the threads are locked.
All services and DAO classes are defined as singletons in Spring application context.
Any idea?
Thanks for your help.
Béranger
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
maxActive
和maxIdle
使用什么值?如果我知道这些值,我可以给您更明确的答案,但同时您也可以尝试将hibernate.dbcp.maxWait
的值从100000
更改为1
并再次测试您的应用程序。还要确保您没有意外地使某些 DAO 方法
同步
。What values are you using for
maxActive
andmaxIdle
? I can give you a more definitive answer if I know those values, but in the meantime you can also try changing the value ofhibernate.dbcp.maxWait
from100000
to1
and test your application again.Also make sure you haven't you accidentally made some of your DAO methods
synchronized
.这是一个奇怪的问题,但我终于解决了我的问题。
这种连接锁定的原因是我在 Spring 中同时使用了声明式和编程式事务管理。我认为我的程序化事务管理是错误的。
以下是用于开始事务的代码:
如您所见,我将程序化事务设置为“必需”。但是,当这些事务在事务上下文中启动时,Spring 无法识别当前事务并启动一个新事务。它导致连接锁定...
我通过删除编程事务管理解决了这个谜团。
This was a weird one but I finally solved my problem.
The reason of this connection locking was that I used both declarative and programmatic transaction management with Spring. I think that my programmatic transaction management was wrong.
Here is the code used to begin a transaction:
As you can see I set the programmatic transactions as “required”. But when those transactions were started in a transactional context, Spring didn’t recognized the current transaction and started a new one. It leaded to a connection locking...
I solved this mystery by removing the programmatic transaction management.