开发者社区> 问答> 正文

使用Hibernate,数据库连接一直莫名断开,求解????????报错

这个RegToChannel_id()就是在Servlet里面的doPost()调用的,其中sessionFactory我是在ini()获取的,

private void RegToChannel_id(HttpServletRequest req, HttpServletResponse resp) throws IOException { // TODO Auto-generated method stub session = sessionFactory.openSession(); mTransaction = session.beginTransaction(); if (session.createQuery(QUERYCHANNELIDISEXIST) .setString("CHANNELID", req.getParameter(CHANNELID)) .uniqueResult() == null){ User u = new User(); u.setChannelid(req.getParameter(CHANNELID)); session.save(u); session.flush(); resp.setStatus(HttpStatusCode.SUCCESS); toJsonMap.put("Success_code","1"); resp.getWriter().print(JsonWrite.JsonFromMap(toJsonMap)); }else{ resp.setStatus(HttpStatusCode.ALREADYEXISTS); toJsonMap.put("Error_code",ErrorMsg.ALREADYEXISTS[0]); toJsonMap.put("Error_msg", ErrorMsg.ALREADYEXISTS[1]); resp.getWriter().print(JsonWrite.JsonFromMap(toJsonMap)); } mTransaction.commit(); session.close(); } @Override public void init() throws ServletException { // TODO Auto-generated method stub sessionFactory = HibernateConfiguration.getSessionFactory(); super.init(); } @Override public void destroy() { // TODO Auto-generated method stub super.destroy(); }
其中HibernateConfiguration是这样的:
public class HibernateConfiguration { private static final SessionFactory sessionFactory; static {
Configuration config = new Configuration().configure(); sessionFactory = config.buildSessionFactory( new ServiceRegistryBuilder() .applySettings(config.getProperties()) .buildServiceRegistry()); } public static SessionFactory getSessionFactory() {
return sessionFactory;
}
现在的情况是不知道为什么,当我访问这个页面时候第一次是可以正确返回的,但是第二次就马上报错了,错误如下:

HTTP ERROR 500

Problem accessing /Reg. Reason:

could not prepare statement


Caused by:

org.hibernate.exception.JDBCConnectionException: could not prepare statement
	at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:67)
	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.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:188)
	at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:159)
	at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1859)
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
	at org.hibernate.loader.Loader.doQuery(Loader.java:900)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
	at org.hibernate.loader.Loader.doList(Loader.java:2526)
	at org.hibernate.loader.Loader.doList(Loader.java:2512)
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
	at org.hibernate.loader.Loader.list(Loader.java:2337)
	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:356)
	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)
	at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
	at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:905)
	at com.lohanry.ntqreg.Reg.RegToChannel_id(Reg.java:87)
	at com.lohanry.ntqreg.Reg.RegToChannel(Reg.java:74)
	at com.lohanry.ntqreg.Reg.doPost(Reg.java:67)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:550)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:489)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:480)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:249)
	at com.baidu.jetty.security.quotalimit.LimitQuotaHandler.doHandle(LimitQuotaHandler.java:64)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1003)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:417)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:200)
	at com.baidu.jetty.security.quotalimit.LimitQuotaHandler.doScope(LimitQuotaHandler.java:43)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:934)
	at org.eclipse.jetty.webapp.WebAppContext.doScope(WebAppContext.java:539)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:226)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
	at org.eclipse.jetty.rewrite.handler.RewriteHandler.handle(RewriteHandler.java:305)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
	at org.eclipse.jetty.server.Server.handle(Server.java:368)
	at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:605)
	at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:1069)
	at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:601)
	at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:214)
	at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:425)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:535)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:529)
	at java.lang.Thread.run(Thread.java:679)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
	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:1013)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
	at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1213)
	at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1205)
	at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4194)
	at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4160)
	at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:161)
	at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
	... 49 more

Caused by:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
	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:1013)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
	at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1213)
	at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1205)
	at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4194)
	at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4160)
	at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:161)
	at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
	at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:159)
	at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1859)
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
	at org.hibernate.loader.Loader.doQuery(Loader.java:900)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
	at org.hibernate.loader.Loader.doList(Loader.java:2526)
	at org.hibernate.loader.Loader.doList(Loader.java:2512)
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
	at org.hibernate.loader.Loader.list(Loader.java:2337)
	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:356)
	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)
	at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
	at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:905)
	at com.lohanry.ntqreg.Reg.RegToChannel_id(Reg.java:87)
	at com.lohanry.ntqreg.Reg.RegToChannel(Reg.java:74)
	at com.lohanry.ntqreg.Reg.doPost(Reg.java:67)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:550)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:489)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:480)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:249)
	at com.baidu.jetty.security.quotalimit.LimitQuotaHandler.doHandle(LimitQuotaHandler.java:64)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1003)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:417)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:200)
	at com.baidu.jetty.security.quotalimit.LimitQuotaHandler.doScope(LimitQuotaHandler.java:43)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:934)
	at org.eclipse.jetty.webapp.WebAppContext.doScope(WebAppContext.java:539)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:226)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
	at org.eclipse.jetty.rewrite.handler.RewriteHandler.handle(RewriteHandler.java:305)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
	at org.eclipse.jetty.server.Server.handle(Server.java:368)
	at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:605)
	at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:1069)
	at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:601)
	at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:214)
	at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:425)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:535)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:529)
	at java.lang.Thread.run(Thread.java:679)
Powered by Jetty://

看起来好像是因为连接断开了,我却还在操作,但是我似乎没有关闭连接?是不是commit()之后就关闭了,但是如果是这样,那么我那段RegChannel_id的代码应该如何修改?第一次使用很久都没有头绪,,,,

还有是我运行的是BAE的环境他说不支持c3p0,和proxpool的连接池,而自带的连接池官方也不推荐使用,那么应该如何解决?谢谢大家帮助我解决问题~

@红薯

















展开
收起
爱吃鱼的程序员 2020-06-22 18:28:49 1023 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    只能自己顶一下了...数据库给的连接时间应该比较短印象中H有个timeout配置参数现在在外面抓节柳龟回去再看看

    atcom.lohanry.ntqreg.Reg.RegToChannel_id(Reg.java:87)
    这是哪一行呢?
    session.createQuery()这句跟着就是说查询时候发现连接断开.

     mTransaction=session.beginTransaction();

    mTransaction不是local变量?

    <atarget='_blank'>@ajavaloser<arel='nofollow'target='_blank'>http://developer.baidu.com/wiki/index.php?title=docs/cplat/rt/java/mysql这个是百度的文档,关于数据库很简单<atarget='_blank'>@Lohanry<aclass='referer'target='_blank'>@Lohanry有相关文档链接吗,CC<atarget='_blank'>@ajavalosergoogle之类均提议是使用连接池,但是bae不支持,不知道怎么解决.回复<aclass='referer'target='_blank'>@Lohanry:担心多线程的问题貌似多余了,毕竟只有你一个人在操作,不知道是不是和你的BAE环境有关系回复<aclass='referer'target='_blank'>@ajavaloser:刚刚试了下,报的错是一样的.你的session也不是local的,贴出变量的声明我倒是找到了一个,<arel="nofollow">http://developer.baidu.com/wiki/index.php?title=docs/cplat/rt/java#Hibernate 下载示例应用sshdemo,你看看,看起来是支持连接池的,但只是spring自带的连接池,可能<spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14px;line-height:22px;background-color:#FFFFFF;">c3p0,proxpool这些太复杂了吧,dbcp有没有试过书上写着意思不用第三方就是自带的了.话说回来,hibernate一定需要连接池吗?看了下hibernatereference,hibernate也有内置连接池的啊,BAE总不会连这都不支持吧<atarget='_blank'>@ajavaloser刚刚搭建完数据库,试了一下,本地是可以连接的,我打算去问下BAE<atarget='_blank'>@Lohanry你现在有错也找不出来啊,所以先在本地试一下不好吗?如果这样的话,我代码是不是有地方错了,一直找不出来.对了,你有没有用本地数据库测过?如果本地没问题,就是BAE环境的问题了呗<atarget='_blank'>@Lohanry<imgsrc="/js/ke/plugins/emoticons/36.gif"alt="36"/>我已经换到了3的版本,但是错误出现了,BAE边也没有给我回复....<atarget='_blank'>@ajavaloser这个我还真给忘记了,不过要明天了,测试下.我现在在win下没有环境.回复<aclass='referer'target='_blank'>@Lohanry:对了,为什么要用ServiceRegistryBuilder?你用的hibernate4吗,BAE似乎支持hibernate3哦刚刚搭建完数据库,试了一下,本地是可以连接的,我打算去问下BAE遇到同样问题,楼主解决了吗?回复<aclass='referer'target='_blank'>@Lohanry:麻烦指导下用什么呢?官方不是支持Hibernate的嘛,再说我们有没有配置数据库连接池。。。好像是BAE的问题,无法使用连接池,后来我就没有用Hibernate,用其他的了

    2020-06-22 18:29:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里云数据库运维实战手册 立即下载
传统数据库上云最佳实践-友邦保险 立即下载
数据库游戏行业最佳实践 立即下载