Spring 和编程式事务管理的连接池问题

发布于 2024-09-14 15:27:53 字数 1967 浏览 7 评论 0原文

我需要你的帮助来解决 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2024-09-21 15:27:53

maxActivemaxIdle 使用什么值?如果我知道这些值,我可以给您更明确的答案,但同时您也可以尝试将 hibernate.dbcp.maxWait 的值从 100000 更改为 1 并再次测试您的应用程序。

还要确保您没有意外地使某些 DAO 方法同步

What values are you using for maxActive and maxIdle? I can give you a more definitive answer if I know those values, but in the meantime you can also try changing the value of hibernate.dbcp.maxWait from 100000 to 1 and test your application again.

Also make sure you haven't you accidentally made some of your DAO methods synchronized.

忆梦 2024-09-21 15:27:53

这是一个奇怪的问题,但我终于解决了我的问题。

这种连接锁定的原因是我在 Spring 中同时使用了声明式和编程式事务管理。我认为我的程序化事务管理是错误的。

以下是用于开始事务的代码:

//Return Spring context
ApplicationContext context = ApplicationContextHolder.getContext();
// transactionManager 
PlatformTransactionManager transactionManager = (PlatformTransactionManager) context.getBean("txManager");
//Set propagation required
DefaultTransactionDefinition td = new DefaultTransactionDefinition();
td.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
// Begin
TransactionStatus transactionStatus = transactionManager.getTransaction(td);

如您所见,我将程序化事务设置为“必需”。但是,当这些事务在事务上下文中启动时,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:

//Return Spring context
ApplicationContext context = ApplicationContextHolder.getContext();
// transactionManager 
PlatformTransactionManager transactionManager = (PlatformTransactionManager) context.getBean("txManager");
//Set propagation required
DefaultTransactionDefinition td = new DefaultTransactionDefinition();
td.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
// Begin
TransactionStatus transactionStatus = transactionManager.getTransaction(td);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文