2012-03-01 3 views
0

최대 절전 모드 세션에서 테이블을 업데이트하려고 시도하고 최대 절전 모드 테이블을 다시 패치하여 로컬 데이터 테이블을 새로 고칩니다. 업데이트를하지 않으면 문제없이 로컬 데이터 테이블을 새로 고칠 수는 있지만 업데이트를 추가하면 서버 로그에 다음 오류가 표시됩니다.왜 내 종은 가까이 간다?

심각도 : org.hibernate.SessionException : 세션이 다음과 같습니다. 닫은!

누구나 내가 뭘 잘못하고 무엇을 할 수있는 문제에 대한 제안을 줄 수 있습니까?

감사합니다.

다음은 업데이트를 수행하는 코드입니다. 예외가 발생하지 않고 테이블의 업데이트 및 커밋을 나타내는 return stat 값 1을 얻었 기 때문에 올바르게 작동하는 것 같습니다.

public int updatePolygonGroupAtrributes(long id, String groupName, String color, long ownerId, int updtUserId) 
{ 
    int stat = 0; 
    TbPolygonGroup polyGroup = null; 
    TbCustAccount custAcct = null; 
    TbUser user = null; 

    try 
    { 
     org.hibernate.Transaction tx = session.beginTransaction(); 
     polyGroup = (TbPolygonGroup)session.get(TbPolygonGroup.class, id); 
     polyGroup.setVcPolygonGroup(groupName); 
     polyGroup.setVcColor(color); 
     custAcct = (TbCustAccount)session.get(TbCustAccount.class, ownerId); 
     System.out.println("<DbHelper.updatePolygonGroupAtrributes> ownerId = "+custAcct.getBiAccountId()+", Name = "+custAcct.getVcCustAccountName()); 
     polyGroup.setTbCustAccount(custAcct); 
     polyGroup.setDtUpdTime(new Date()); 
     user = (TbUser)session.get(TbUser.class, updtUserId); 
     System.out.println("<DbHelper.updatePolygonGroupAtrributes> update User Id = "+user.getIuserId()+", Name = "+user.getVcUserFirstName()+" "+user.getVcUserLastName()); 
     polyGroup.setTbUser(user); 
     try 
     { 
      session.update(polyGroup); 
      tx.commit(); 
      stat = 1; 
     } 
     catch(Exception e) 
     { 
      tx.rollback(); 
      e.printStackTrace(); 
      status = e.getMessage(); 
      stat = -1; 
     } 
    } 
    catch(Exception ex) 
    { 
     ex.printStackTrace(); 
     status = ex.getMessage(); 
     stat = -1; 
    } 
    return stat; 
} 

내 응용 프로그램에서 업데이트를 호출하는 코드는 다음과 같습니다 (내가하는 일의 흐름을 보여주는 것 외에는별로 관련이 없음). 여기

public void polyGroupUpdateAction() 
{ 
    int changed = 0; 
    String clr = "#"+polyColor; 
    long ownId = sb1.getLong(selectedPolyOwner, 0); 
    long groupId = selectedPolygonGroup.getBiPolygonGroupId(); 
    int updUserId = sb1.getLoginUserId(); 
    int retVal = dbHelper.updatePolygonGroupAtrributes(groupId, polyName, clr, ownId, updUserId); 
    if(retVal < 0) 
    { 
     sb1.setMessage("Error updating Polygon Group: "+dbHelper.getStatus()); 
     return; 
    } 
    System.out.println("---------<DeviceSessionBean.polyGroupUpdateAction> Return value = "+retVal); 
    sb1.setMessage("Polygon Group has been updated."); 
    fillPolyGroupList(); 
    return; 
} 

는 (오류가 발생한 경우 방송) 나는 내 로컬 테이블을 보충 관련 코드 여기
public void fillPolyGroupList() 
{ 
    System.out.println("<DeviceSessionBean.fillPolyGroupList> Entering ... "); 
    tbPolygonGroupList = dbHelper.getPolygonGroup(); 
    System.out.println("<DeviceSessionBean.fillPolyGroupList> After reloading polygon group list ... "); 
    polygonGroupDataModel = new PolygonGroupDataModel(tbPolygonGroupList); 
} 

업데이트가 성공적으로 반환을 보여줍니다 세션 로그입니다 만, 오류 때 제공 fillPolyGroupList()에서 데이터 테이블을 다시 읽으려고 시도합니다. 당신이 세션에서 트랜잭션을 커밋 할 때

INFO: <DbHelper.updatePolygonGroupAtrributes> ownerId = 74, Name = TransCore - David Kerr 
INFO: <DbHelper.updatePolygonGroupAtrributes> update User Id = 2, Name = Transcore System 
INFO: ---------<DeviceSessionBean.polyGroupUpdateAction> Return value = 1 
INFO: <DeviceSessionBean.fillPolyGroupList> Entering ... 
SEVERE: org.hibernate.SessionException: Session is closed! 
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49) 
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1319) 
..... 
at java.lang.Thread.run(Thread.java:662) 

INFO: <DeviceSessionBean.fillPolyGroupList> After reloading polygon group list ... 
WARNING: #{deviceSessionBean.polyGroupUpdateAction}: java.lang.NullPointerException 
javax.faces.FacesException: #{deviceSessionBean.polyGroupUpdateAction}: java.lang.NullPointerException 
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) 
at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
.... 

답변

0

는 Hibernate는 은 자동으로 다시 사용할 수 없습니다 의미 세션을 닫습니다. 내 링크에

http://www.17od.com/2006/11/06/using-managed-sessions-in-hibernate-to-ease-unit-testing/


은 또한 수동으로 세션을 제어하는 ​​기능에서 테스트 한 다음에 새로운 경우 모든 :)


을 수정하는 방법에 대해 설명합니다 최대 절전 모드 일 경우, 당신은 이미 구성된 최대 절전 모드/봄 예제와 함께 괜찮은 프로젝트 구조를 제공 할 maven 아키 타입에서 프로젝트를 시작하는 것이 좋습니다.

maven을 설치하고 mvn archetype : generate을 입력하고 사용할 테크놀로지를 선택하십시오. 그런 다음 mvn eclipse : eclipse를 입력하여 새 프로젝트에서 Eclipse 구성 파일을 생성하고 마지막으로 Eclipse로 가져옵니다. 전체 스택을 설정하여 시작하는 데 도움이됩니다. (예 : appfuse-basic-spring은 SpringMVC, Spring 및 Hibernate를 사용하는 응용 프로그램을 만듭니다.)

+0

Btw wtf는 dbHelper입니까? 우리는 현재 dao를 가지고 있습니다. –

+0

저는 최대 절전 모드에 익숙하지 않습니다. 그리고 이것을 netbeans에 대한 최대 절전 모드 튜토리얼에서 보았습니다. 또한 당신이 참조한 페이지를 보았고 업데이트를하지 않는 데이터를 가져 오는 데 필요한 50 가지 이상의 모든 기능을 다시 작성하지 않아도되기를 바랬습니다. – Burferd

1

제안 된 솔루션을 시도하고 "스레드"대신 "수동"모드로 변경하고 openSession() 및 closeSession) 내 최대 절전 모드 액세스하는 각각의 96 방법 주위에. 그것은 작동하지 않았다. 액세스 메소드에 대한 두 번 호출 후 "세션 없음"오류가 발생했습니다. 그 솔루션은 어떤 이유로 작동하지 않았습니다. 96 가지 방법 중 3 가지만이 실제로 업데이트를 수행했기 때문에 불필요한 오버 헤드를 많이 발생 시켰습니다.

그래서 모든 메소드를 열기/닫기 세션 코드로 감싸기보다는 commit() 후에 세션을 다시 열어 적절한 결과를 얻는 것을 발견했습니다.나는 위의 코드에서

라인이 있습니다

tx.commit(); 

가 난 그냥이 후()는 OpenSession에 추가하면, 나는 내가 찾고있는 결과를 얻을 수 있음을 발견했다.

tx.commit(); 
this.session - HibernateUtil.getSessionFactory().openSession(); 

이 난 단지 (이 시점에서) 3이를 추가해야 할이 시점에서 실제로 업데이트 할 방법과 다른 93 (: 그래서 위의 코드는 위의 tx.commit()로 변경되었습니다) 메소드는 쓸데없이 남아있을 수 있습니다.