Hibernate - 数据库在几次查询后停止响应

时间:2017-03-02 10:26:25

标签: java hibernate java-ee

我正在使用Hibernate 5.2.6和Tomcat服务器,以使queriesto成为一个MySQL数据库。它会一直处于罚款状态,直到应用程序达到7次调用数据库。

无论调用是什么(getAll(),find(),update(),海关方法),数据库在7次调用后都会停止响应。这不是时间问题,因为我可以让应用程序运行几分钟而不会崩溃。但是一旦拨打7个电话,它就会挂起。没有错误消息。

以下是在服务器控制台中显示的查询8,没有来自数据库的响应:

enter image description here

以下是我调用的两个服务(第一个是由Hibernate生成的;我创建了第二个):

@Override
public List<E> getAll() {
if (!currentSession().getTransaction().isActive()) {
    currentSession().beginTransaction();
}
List<E> ret = currentSession().createCriteria(daoType).list();
return ret;
}

@Override
public List<Subcategory> findByCategory(Category cat) {

if (!currentSession().getTransaction().isActive()) {
    currentSession().beginTransaction();
}

Query query = currentSession().createQuery("from Subcategory where category = :category");
query.setParameter("category", cat);
List<Subcategory> listSub = query.list();
return listSub;
}

我打电话的服务有问题吗?或者这是服务器配置问题吗?

感谢您的帮助!

修改

以下是我改变方法的示例:

@Override
public Audit findAuditById(int id) {

    if (!currentSession().getTransaction().isActive()) {
        currentSession().beginTransaction();
    }

    Query query = currentSession().createQuery("from Audit where idAudit = :id");
    query.setParameter("id", id);

    Audit audit = (Audit) query.uniqueResult();

    Hibernate.initialize(audit);
    currentSession().getTransaction().commit(); 
    currentSession().close();

    return audit;
}

这是我得到的“无会话”错误:

 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server
 faultSubcode: 
 faultString: Marshalling Error: could not initialize proxy - no Session
 faultActor: 
 faultNode: 
 faultDetail: 
    {http://xml.apache.org/axis/}stackTrace:Marshalling Error: could not initialize proxy - no Session
    at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
    at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
    at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327)
    at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
    at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
    at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
    at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
    at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
    at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
    at org.apache.axis.client.Call.invoke(Call.java:2767)
    at org.apache.axis.client.Call.invoke(Call.java:2443)
    at org.apache.axis.client.Call.invoke(Call.java:2366)
    at org.apache.axis.client.Call.invoke(Call.java:1812)
    at com.safran.engineering.esurvey.ws.impl.EsurveyWebServiceImplServiceSoapBindingStub.getAuditById(EsurveyWebServiceImplServiceSoapBindingStub.java:411)
    at model.impl.ImpGUI_Home.getAuditById(ImpGUI_Home.java:160)
    at model.Abstract_Home.lambda$1(Abstract_Home.java:77)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:0)

如果你所做的只是检索一个列表,那么你不需要启动一个事务。如果要删除,更新或插入记录,则只需要事务。一般情况下,如果您正在(通常)在同一代码块中启动事务,我希望看到提交或回滚。我不确定MySQL是否支持嵌套事务,因此可能会导致问题。

或者,如果MySQL本身正在停止(你知道如果你不能从MySQL Workbench连接到它,你会知道因为它不仅仅是你的应用程序需要重新启动),那么看一下mysql生成的日志文件,因为这可能会给出一个关于原因的线索。如果出现硬件错误,那可能只是一个问题,但这只是一种感觉。

答案 1 :(得分:0)

正如Brian所说,你不需要选择查询的交易。

如果您仍想拥有它们,则需要提交事务并关闭会话。然后调用initialize方法以避免延迟加载问题。

    @Override
public List<E> getAll() {
    if (!currentSession().getTransaction().isActive()) {
        currentSession().beginTransaction();
    }
    List<E> ret = currentSession().createCriteria(daoType).list();
    // Call initiliaze to avoid lazy loading exception.
    Hibernate.initialze(ret);
    // You need to commit the transaction & close the session.
    currentSession().getTransaction().commit(); 
    currentSession().close();
    return ret;
}