gpt4 book ai didi

java - 使用 hibernate 模板时 MySql 连接过多?

转载 作者:行者123 更新时间:2023-11-30 22:12:35 25 4
gpt4 key购买 nike

在对 mysql 进行 100 次点击后,我们得到 com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException:数据源拒绝建立连接,来自服务器的消息:“连接太多”。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/TechDB?createDatabaseIfNotExist=true"></property>
<property name="username" value="xxxxx"></property>
<property name="password" value="xxxxx"></property>
<property name="initialSize" value="30" />
<property name="maxActive" value="100" />
<property name="maxWait" value="5" />
<property name="maxIdle" value="1" />

</bean>

<bean id="mysessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>jobseeker.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</prop>
<prop key="hibernate.c3p0.min_size">1</prop>
<prop key="hibernate.c3p0.max_size">19</prop>
<prop key="hibernate.c3p0.timeout">120</prop>
<prop key="hibernate.c3p0.max_statements">10</prop>
<prop key="hibernate.connection.release_mode">after_statement</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
</props>
</property>
</bean>

<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
</bean>

最佳答案

在您的 java 代码中,一旦您为请求提供服务,请确保连接已关闭。

Connection connection = null;
try {
connection = database.getConnection();
// the actual code goes here
} finally {
if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}

关于java - 使用 hibernate 模板时 MySql 连接过多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39588164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com