session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.save(admin);
tx.commit();
}catch(HibernateException e){
throw e;
}finally{
if (tx!=null) {
tx.rollback();
}
HibernateSessionFactory.closeSession()
}
commit 已经成功了,为什么老提示Transaction not successfully started
而且单独写类测试都通过的,加上struts的action就问题很多
tx = session.beginTransaction();
session.save(admin);
tx.commit();
}catch(HibernateException e){
throw e;
}finally{
if (tx!=null) {
tx.rollback();
}
HibernateSessionFactory.closeSession()
}
commit 已经成功了,为什么老提示Transaction not successfully started
而且单独写类测试都通过的,加上struts的action就问题很多
HibernateSessionFactory
package com;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory { private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; /** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal(); /** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration(); /** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory; /**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* Default constructor.
*/
private HibernateSessionFactory() {
}}
tx.rollback();
}
这个放这干吗的?每次commit完还要rollback么?
tx = session.beginTransaction();
session.save(admin);
tx.commit();
}catch(HibernateException e){
throw e;
if (tx!=null) {
tx.rollback();
}
}finally{
HibernateSessionFactory.closeSession()
}