2014-09-22 6 views
2

최대 절전 모드와 개찰구를 배우려고하는 임. 실제로 절전 세션을 열고 닫을 때 확실하지 않습니다. 저는 세션 팩토리에 대해 많은 것을 검색하고 많은 것을 읽었습니다. 그러나 저는 여전히 그것을 얻지 못합니다.최대 절전 모드 세션 처리

데이터베이스의 일부 데이터를 가져 와서 브라우저의 테이블에 표시하고 싶습니다. 해당 사이트에 처음으로 메신저 경우 즉 실제로 작동하지만, 내가 다시 그 사이트에 뒤로 버튼을 사용하여 가면 그것은 나에게이 오류 보여줍니다 : 나는 조기 또는 뭔가 내 SessionFactory를 닫습니다 때문에

Last cause: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] 

내가 그 생각을 그런 식으로. 하지만 문제를 해결하는 방법을 모른다.

내 자바 클래스 :

public class CategoryPanel extends Panel { 
    private WebMarkupContainer categoryContainer; 
    public CategoryPanel(String id) { 
     super(id); 
     SessionFactory sessionFactory = DbFactory.getSessionFactory();     // creating the Session Factory  
     StandardDao<Category> categoryDao = StandardDao.getInstance(sessionFactory); // creating dao to access data 
     List<Category> categoryList = categoryDao.getAll(Category.class);    // get data of the db 
     CategoryDataProvider dataProvider = new CategoryDataProvider(categoryList);   
     categoryContainer = new WebMarkupContainer("categoryTable"); 

     final DataView dv = new DataView("categoryList", dataProvider) { 

      @Override 
      protected void populateItem(Item item) { 
       final Category category = (Category) item.getModelObject(); 
       final CompoundPropertyModel<Category> categoryModel = new  CompoundPropertyModel<Category>(category); 

       item.add(new Label("catTitle", categoryModel.bind("title"))); 
      } 
     }; 
     categoryContainer.add(new Label("categoryTitle", Model.of("Titel"))); 
     add(categoryContainer); 
     sessionFactory.close(); // here I close the factory, this seems to be wrong. I dont know if i close it anyway.. 
    } 
} 

내 다오 :

public class StandardDao<T> { 
private static StandardDao instance = null; 
    private static final Object syncObject = new Object(); 
    private SessionFactory sessionFactory; 


    private StandardDao(SessionFactory session){ 
    this.sessionFactory = session; 
    } 

    public static StandardDao getInstance(SessionFactory session) { 
     if (instance == null) { 
      synchronized (syncObject) { 
       if (instance == null) { 
        instance = new StandardDao(session); 
       } 
      } 
     } 
     return instance; 
    } 
     public List<T> getAll(Class theClass) { 
      List<T> entity = null; 
      Session session = sessionFactory.openSession(); 
      try { 
       entity = session.createCriteria(theClass).list(); 
      } catch (RuntimeException e) { 
       e.printStackTrace(); 
      }finally { 
      session.flush(); 
      // if I close the session here, I cant load lazy 
     } 
      return entity; 
     } 
    } 

답변

4

당신은 SessionFactory를 닫지한다. SessionFactory는 당신이 일하는 Session을 리턴하고 Session 만 닫는 데 사용됩니다.

또한 일반적으로 컨트롤러 코드 내에서 세션을 열고 닫지 않지만 요청이 도착할 때마다 세션 팩토리에서 새 세션을 열 수있는 외부 구성 요소에이 처리를 위임하고 가장 중요한 것은 요청이 완료되면보기 부분도 작업을 완료했음을 의미합니다.

이 패턴은 일반적으로 "보기에서 열린 세션"이라고합니다.

Java 웹 서버에서 이것은 일반적으로 web.xml에서 구성하는 서블릿 필터로 수행됩니다.

Google에서 Java 열기 세션보기 필터를 검색하면 많은 예제와 설명을 찾을 수 있습니다.

관련 문제