用hibernate插一些数据,但是只能一个一个插,一起插到时候,第一个数据是成功插入的,从第二个开始就不能成功插入,报的是:Transaction not successfully started
HibernateUtil类是这样写的:
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
static{
try {
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static Session getSession(){
Session session=threadLocal.get();
if(session==null||!session.isOpen()){
session=(sessionFactory!=null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
public static void closeSession(){
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if(session!=null){
session.close();
}
}
业务逻辑层管理事务:
public class TypeBizImpl implements TypeBiz{
private BaseDAO tDAO=new TypeDAO();
private Transaction tx=HibernateUtil.getSession().beginTransaction();
@Override
public void addType(Filmtype type) {
// TODO Auto-generated method stub
try {
tDAO.add(type);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
不知道哪里错了
HibernateUtil类是这样写的:
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
static{
try {
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static Session getSession(){
Session session=threadLocal.get();
if(session==null||!session.isOpen()){
session=(sessionFactory!=null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
public static void closeSession(){
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if(session!=null){
session.close();
}
}
业务逻辑层管理事务:
public class TypeBizImpl implements TypeBiz{
private BaseDAO tDAO=new TypeDAO();
private Transaction tx=HibernateUtil.getSession().beginTransaction();
@Override
public void addType(Filmtype type) {
// TODO Auto-generated method stub
try {
tDAO.add(type);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
不知道哪里错了
解决方案 »
- spring 的annotation
- smartupload无法上传图片
- hibernate 的问题
- TOMCAT启动时提示IP_ADD_MEMBERSHIP failed错误
- jsp中text赋值?
- jsp 调 InetAddress 的 isReachable(2000) 怎么结果都是投入true? 求高手指点
- 不好意思com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp5,wKjRELB7HAFeDYtGyubPBA==.30329S2 1183518
- 如何产生一个随机整数.
- 关于tomcat连接池的问题,在线急等
- 先执行mvn clean在执行mvn install 和mvn clean install有什么区别吗
- 关于tomcat多域名的问题
- 关于JavaBean问题....新手,折腾得快疯掉了...
@Override
public void addType(Filmtype type) {
// TODO Auto-generated method stub
try {
tDAO.add(type);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
改成private Transaction tx=null;
@Override
public void addType(Filmtype type) {
// TODO Auto-generated method stub
HibernateUtil.getSession().beginTransaction();try {
tDAO.add(type);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
@Override
public void addType(Filmtype type) {
// TODO Auto-generated method stub
tx= HibernateUtil.getSession().beginTransaction();try {
tDAO.add(type);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
因为每个Transaction 提交后要重新 开始才有用