我正在使用Hibernate 4和mysql作为我当前的网站,在我尝试在2个网页浏览器的标签页中多次刷新页面后,服务器出现了错误日志:
ERROR: Data source rejected establishment of connection, message from server: "Too many connections"
org.hibernate.exception.JDBCConnectionException: Could not open connection
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:68)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
at hibernate.dao.StockDao.getWarehouseInStock(StockDao.java:132)
at Utils.Common.updateStockHistorical(Common.java:501)
at Controller.StockServlet.processRequest(StockServlet.java:67)
at Controller.StockServlet.doPost(StockServlet.java:116)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at Controller.ServletFilter.doFilter(ServletFilter.java:134)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
at java.lang.Thread.run(Thread.java:722)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
at sun.reflect.GeneratedConstructorAccessor70.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1014)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1104)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2412)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2230)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor62.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:190)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:279)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
... 24 more
这是我对hibernate的配置:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/wstrader2?characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.current_session_context_class">jta</property>
<mapping resource="hibernate/dto/Warehouse.hbm.xml"/>
<mapping resource="hibernate/dto/Exchange.hbm.xml"/>
<mapping resource="hibernate/dto/Packing.hbm.xml"/>
<mapping resource="hibernate/dto/GradeMaster.hbm.xml"/>
<mapping resource="hibernate/dto/MarketFobDiff.hbm.xml"/>
<mapping resource="hibernate/dto/Arbitration.hbm.xml"/>
<mapping resource="hibernate/dto/Quality.hbm.xml"/>
<mapping resource="hibernate/dto/Region.hbm.xml"/>
<mapping resource="hibernate/dto/City.hbm.xml"/>
<mapping resource="hibernate/dto/Contract.hbm.xml"/>
<mapping resource="hibernate/dto/HistoricalBasis.hbm.xml"/>
<mapping resource="hibernate/dto/Country.hbm.xml"/>
<mapping resource="hibernate/dto/Origin.hbm.xml"/>
<mapping resource="hibernate/dto/WarehouseReceipt.hbm.xml"/>
<mapping resource="hibernate/dto/DailyBasis.hbm.xml"/>
<mapping resource="hibernate/dto/User.hbm.xml"/>
<mapping resource="hibernate/dto/Movement.hbm.xml"/>
<mapping resource="hibernate/dto/PaymentTerm.hbm.xml"/>
<mapping resource="hibernate/dto/ContractTerm.hbm.xml"/>
<mapping resource="hibernate/dto/CompanyMaster.hbm.xml"/>
<mapping resource="hibernate/dto/ProcessingOrder.hbm.xml"/>
<mapping resource="hibernate/dto/Stock.hbm.xml"/>
<mapping resource="hibernate/dto/HistoricalStock.hbm.xml"/>
<mapping resource="hibernate/dto/Menu.hbm.xml"/>
<mapping resource="hibernate/dto/Authorization.hbm.xml"/>
<mapping resource="hibernate/dto/Page.hbm.xml"/>
<mapping resource="hibernate/dto/ContractPriceFixation.hbm.xml"/>
<mapping resource="hibernate/dto/FuturesContract.hbm.xml"/>
<mapping resource="hibernate/dto/FutureCloseOut.hbm.xml"/>
<mapping resource="hibernate/dto/Currency.hbm.xml"/>
<mapping resource="hibernate/dto/FuturesContractArchive.hbm.xml"/>
<mapping resource="hibernate/dto/FutureCloseOutArchive.hbm.xml"/>
<mapping resource="hibernate/dto/ScreenMaster.hbm.xml"/>
<mapping resource="hibernate/dto/QualityMaster.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这是我的HibernateUtil.java:
public class HibernateUtil {
private static final SessionFactory ourSessionFactory;
private static ServiceRegistry serviceRegistry;
private static Configuration configuration = new Configuration().configure();
static {
try {
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
ourSessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}
}
有人可以找出原因或告诉我导致错误的条件"Too many connection"
和org.hibernate.exception.JDBCConnectionException: Could not open connection
我试图找到相关主题,但仍未找到我的问题的答案
感谢您阅读此主题。
已更新: 我的样本Dao.java:
public class CityDAO {
private Session session = null;
public CityDAO() {
session = HibernateUtil.getSession();
}
public ArrayList<City> getAllCities() {
try {
session.getTransaction().begin();
Criteria crit = session.createCriteria(City.class);
ArrayList<City> cities = (ArrayList<City>)crit.list();
session.flush();
session.getTransaction().commit();
return cities;
} catch (Exception e) {
if (session.getTransaction().isActive()) {
session.getTransaction().rollback();
}
e.printStackTrace();
}
return null;
}
public City getCityById(int id){
try {
session.getTransaction().begin();
Criteria crit = session.createCriteria(City.class);
crit.add(Restrictions.eq("id", id));
City city = (City)crit.uniqueResult();
session.flush();
session.getTransaction().commit();
return city;
} catch (Exception e) {
if (session.getTransaction().isActive()) {
session.getTransaction().rollback();
}
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:2)
我找到了解决问题的方法。 因为我的Web应用程序使用servlet - 旧式的。所以我试试这个:
https://community.jboss.org/wiki/OpenSessionInView
这对我有用。
谢谢你们给我建议:D
答案 1 :(得分:0)
我假设,MySql抛出了异常。要么增加那里的maxconnections,要么在资源泄漏的情况下检查源代码。例如,不要在每次请求时创建连接实例,也不要在每次请求后关闭连接等等。
答案 2 :(得分:0)
您的DAO类的每个实例都有一个打开的会话,即使您设置了连接池,它也没什么用处。
你做了类似每个应用程序的多个会话反模式(与session-per-application略有不同)。这种反模式存在问题,如Hiberhate Guide
中所述会话不是线程安全的。同时工作的东西,比如HTTP 请求,会话bean或Swing worker将导致竞争条件 如果共享一个Session实例。
Hibernate引发的异常意味着你必须回滚你的 数据库事务并立即关闭Session
另外,因为你没有连接池,当连接被服务器杀死时,应用程序无法检测到情况。