对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>
答案 0 :(得分:0)
在您的Java代码中,一旦您提供请求,请确保连接已关闭。
Connection connection = null;
try {
connection = database.getConnection();
// the actual code goes here
} finally {
if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}
答案 1 :(得分:0)
在MySQL中,默认的max_connections值为151.如果您想要更多连接。
你可以在/etc/my.cnf
中设置You can increase this value in main config file (e.g., /etc/my.cnf) using this syntax:
[mysqld]
set-variable
max_connections=5000
有关详细信息,请参阅以下链接:Mysql Doc(http://dev.mysql.com/doc/refman/5.7/en/too-many-connections.html)