我通过动态创建具有以下属性的数据源来连接mysql数据库,它工作正常,但过了一段时间后,它一直给我错误“无法获取JDBC连接”。
package com.test.db;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import java.util.HashMap;
@Configuration
@EnableJpaRepositories(basePackages = "com.test.master", entityManagerFactoryRef = "userMasterEntityManager", transactionManagerRef = "userMasterTransactionManager")
public class MasterDBConfig {
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean userMasterEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.test.master" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "create");
properties.put("hibernate.id.new_generator_mappings", "false");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.put("hibernate.testOnBorrow", "true");
properties.put("hibernate.validationQuery", "SELECT 1");
properties.put("hibernate.testWhileIdle", "true");
// properties.put("hibernate.timeBetweenEvictionRunsMillis", "3600000");
properties.put("hibernate.connection.autoReconnect", "true");
properties.put("hibernate.connection.autoReconnectForPools", "true");
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean(name = "dataSource")
public DataSourceRouter dataSource() {
return new DataSourceRouter();
}
@Primary
@Bean
public PlatformTransactionManager userMasterTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(userMasterEntityManager().getObject());
return transactionManager;
}
}
它被托管在AWS和Tomcat环境中。DB是AWS的RDS。
更新:
原因:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1710)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor39.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:732)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:664)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:479)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)
对我来说,通过确保所有会话在使用后立即正确关闭,问题得以解决。
您的连接太多,导致无法建立任何新连接。解决方案非常简单——您必须设置连接池(c3p0非常流行)并指定池连接了解如何设置c3p0连接,也不要忘记包含依赖项
我有一个Hibernate和光数据源的Spring Boot项目。如果我使用注入的SessionFactory对象来获取会话对象,几天后,我会发现与数据库操作相关的任何方法都有这样的异常(只有重新启动才能解决这个问题): 似乎手动使用的会话使此问题。(我有类似的项目,具有相同的配置和功能,但没有注入SessionFactory和Session...我根本没有这样的问题) 应用程序. yaml: 数
我们的项目中有Spring boot/Hibernate/MYSQL应用程序,并使用Hikari作为连接池。服务启动几分钟后,我们发现以下问题: org.springframework.web.util.嵌套异常:请求处理失败;嵌套异常org.springframework.dao.DataAccessResourceFailureExc0019:无法获取JDBC连接;嵌套异常org.hibern
我正在运行一个带有Hibernate和Spring数据的Spring启动应用程序。我有以下方法: 当我第四次运行此方法时,我得到了无法获取 JDBC 连接异常。我怀疑当我调用存储过程“callEncrypt”时,Hibernate在作业完成后不会释放连接,因为如果我删除该行,应用程序就可以完美运行,无法手动关闭连接,因为Hibernate正在处理事务(并且没有理由这样做),所以我已经卡住了一段时间
当我使用Japser报告生成报告时,我的Spring Boot工作正常。 我面临的问题是应用程序抛出Hibernate异常: 无法获取JDBC连接 在我多次生成报告后,我得到了这个错误。
有人能告诉我为什么来自MySQL的数据源没有连接。谢谢!