我有這樣的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)
你得到任何堆棧跟蹤? – Divya
不幸的是,沒有。 – ndo
你沒有添加catch塊到大嘗試block.try調試使用,並在那裏添加stacktrace – Divya