java 如何解决异常“com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:数据源拒绝建立连接”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25600907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:18:57  来源:igfitidea点击:

How to resolve the Exception “com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection”?

javamysqlhibernatejdbc

提问by Raghu

my application uses Hibernate. I am working on an ITS(Intelligent Transport System) So here every 10 seconds I have track the position(latitude, longitude) of bus. I call a ajax method every 10 sec which makes request to servlet. My application works fine for some time period, after that it shows error as ,

我的应用程序使用 Hibernate。我正在研究 ITS(智能交通系统),所以这里每 10 秒我就跟踪一次公交车的位置(纬度、经度)。我每 10 秒调用一个 ajax 方法,它向 servlet 发出请求。我的应用程序在一段时间内工作正常,之后它显示错误,

org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
at 

and

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)

I am not getting how to resolve it. I searched many resource and found that it is about managing connection pool in hibernate configuration file.

我不知道如何解决它。我搜索了很多资源,发现它是关于在休眠配置文件中管理连接池的。

So here is my Hiberenate configuration file.

所以这是我的 Hiberenate 配置文件。

<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bus_serverdb</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Disable the second-level cache  -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->

<!-- Names the annotated entity class -->

Can any one tell me what are the changes I have to do and why?

谁能告诉我我必须做哪些改变,为什么?

回答by mp911de

The server responds with "Too many connections"

服务器响应“连接过多”

See http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html

http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html

回答by zatenzu

Make sure that you close correctly your session in the end of your method process in your Servlet.

确保在 Servlet 中的方法进程结束时正确关闭会话。