首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

!org.hibernate.TransactionException: Transaction not successfully started

2012-10-06 
求助!org.hibernate.TransactionException: Transaction not successfully started之前使用测试还好好的

求助!org.hibernate.TransactionException: Transaction not successfully started
之前使用测试还好好的 今天一试莫名出现问题!

package org.whatisjava.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.whatisjava.domain.Category;
import org.whatisjava.util.HibernateSessionFactory;

public class CategoryDaoImpl implements CategoryDao {

public Category getCategoryById(Integer id) {
Session s = HibernateSessionFactory.getSession();
Category c = null;
try {
s.beginTransaction();
c = (Category) s.get(Category.class, id);
s.getTransaction().commit();
} catch (HibernateException e) {
s.getTransaction().rollback();
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}

return c;
}

public Category getCategoryByIdFetch(Integer id) {
return null;
}

public Category findById() {

return null;
}

}


结果报异常:

org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
at org.whatisjava.dao.CategoryDaoImpl.getCategoryById(CategoryDaoImpl.java:18)
at org.whatisjava.controller.MyServletContext.contextInitialized(MyServletContext.java:22)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4336)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:761)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:741)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1023)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)


package org.whatisjava.controller;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.whatisjava.dao.CategoryDao;
import org.whatisjava.dao.DaoFactory;
import org.whatisjava.domain.Category;

public class MyServletContext implements ServletContextListener
,ServletContextAttributeListener{

public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("系统关闭....");
}

public void contextInitialized(ServletContextEvent context) {
System.out.println("系统启动....");


CategoryDao cdao = DaoFactory.getCategoryDao();
Category category = cdao.getCategoryById(1);
context.getServletContext().setAttribute("category", category);
}

public void attributeAdded(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub

}

public void attributeRemoved(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub

}

public void attributeReplaced(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub

}

}


为什么会出现这种情况?

[解决办法]
HibernateSessionFactory是一个单利吧?

如果是它只产生一份session,关闭了就没了,得重新加载!

所以第一次运行可能没问题,第二次进去时就报这个错了!!!

热点排行