2014-03-25 127 views
0

我有這樣的DAO類,它通過Hibernate的數據庫連接:Servlet和Hibernate的 - 不會打開會話

public class ChartDAO { 

public static ChartData getChartData(){ 
    Session session = null; 
    List data; 
    try { 
     try { 
      session = HibUtil.getSessionFactory().openSession(); 
      Transaction tx = session.beginTransaction(); 
      data = session.createQuery("FROM ChartData").list(); 
      tx.commit(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      data = new ArrayList(); 
      data.add(new ChartData()); 
     } 
    } finally { 
     session.disconnect(); 
     session.close(); 
     try { 
      HibUtil.close(); 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 
    ChartData chartData = (ChartData) data.get(0); 
    return chartData; 
} 

public static void main(String[] args) throws Exception{ 
    ChartData cd = getChartData(); 
} 
} 

當我運行的主要方法,在其中我調用getChartData(),一切工作正常(在調試器中檢查 - 會話被打開並且正在檢索數據)。

但是,當我試圖從servlet調用相同的方法時,數據不被檢索。我使用調試器進行了檢查 - 它在試圖打開休眠會話時「凍結」了一會兒,然後立即跳轉到「finally」塊。

我在做什麼錯?

謝謝。

編輯:下面是我的HibUtil:

public class HibUtil { 

private static ServiceRegistry serviceRegistry; 
private static final SessionFactory sessionFactory; 

static { 
    try { 
     Configuration configuration = new Configuration(); 
     configuration.configure(); 
     serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 
     sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
    } catch (Throwable ex) { 
     System.err.println("Initial SessionFactory creation failed." + ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
} 

public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

public static void close() throws Exception{ 
    if(serviceRegistry!= null) { 
     StandardServiceRegistryBuilder.destroy(serviceRegistry); 
    } 
} 
} 

UPDATE:這是Apache Tomcat的日誌,當我嘗試調用從servlet方法:

mar 25, 2014 6:42:41 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [DataProvider] in context with path [/SHJS] threw exception 
java.lang.NullPointerException 
    at org.mb.DataProvider.processRequest(DataProvider.java:53) 
    at org.mb.DataProvider.doGet(DataProvider.java:83) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:722) 
+0

你得到任何堆棧跟蹤? – Divya

+0

不幸的是,沒有。 – ndo

+0

你沒有添加catch塊到大嘗試block.try調試使用,並在那裏添加stacktrace – Divya

回答

0

使用會話

創建連接
Connection connection  = session.connection(); 

然後使用connection.as創建您的查詢您沒有創建連接,所以它migh不工作。 您也可以使用Annotation Configuration 這樣

private static SessionFactory buildSessionFactory() { 
    try { 
     // Create the SessionFactory from hibernate.cfg.xml 
     return new AnnotationConfiguration().configure() 
       .buildSessionFactory(); 
    } catch (Throwable ex) { 
     System.err.println("Initial SessionFactory creation failed." + ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
} 
public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

並稱之爲

Session session = HibernateUtil.getSessionFactory().openSession(); 

使session.then事務,然後連接等。本爲我工作得很好,你可以試試。

+0

我無法使用創建連接,而我甚至無法打開會話! 問題是,會話從任何地方(任何方法,任何類)成功創建...除了從servlet調用它:( – ndo

+0

是啊先創建會話,然後連接,我提到使會話然後session.conncetion – Divya

+0

但它不會改變任何東西 - 當我從一個servlet調用這個方法時,'session'仍然不會被創建(但是當我從其他地方調用該方法時問題不會出現)。 – ndo

0

試試這個:

new Configuration().configure().buildSessionFactory();