exception javax.servlet.ServletException: Transaction not successfully started
root cause org.hibernate.TransactionException: Transaction not successfully started
相关Transaction的代码
在AdminDAOFactory.java中
try{
session = HibernateSessionFactory.getSession();
tx = session.getTransaction();
session.save(admin);
tx.commit();
}
catch(HibernateException e){
throw e;
}
finally{
HibernateSessionFactory.closeSession();
}
在AddAdminForm.java中ActionErrors errors = new ActionErrors();
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("select admin from Admin as admin where admin.id.name = '"+ this.name + "'");
Iterator it = query.iterate();
if (it.hasNext()) {
errors.add("addAdmin.err.name", new ActionMessage("form.addAdmin.err.name"));
}
tx.commit();
HibernateSessionFactory.closeSession();
return errors;
root cause org.hibernate.TransactionException: Transaction not successfully started
相关Transaction的代码
在AdminDAOFactory.java中
try{
session = HibernateSessionFactory.getSession();
tx = session.getTransaction();
session.save(admin);
tx.commit();
}
catch(HibernateException e){
throw e;
}
finally{
HibernateSessionFactory.closeSession();
}
在AddAdminForm.java中ActionErrors errors = new ActionErrors();
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("select admin from Admin as admin where admin.id.name = '"+ this.name + "'");
Iterator it = query.iterate();
if (it.hasNext()) {
errors.add("addAdmin.err.name", new ActionMessage("form.addAdmin.err.name"));
}
tx.commit();
HibernateSessionFactory.closeSession();
return errors;
session.getTransaction()换成Transaction tx = session.beginTransaction()。。
这两个方法的返回值都是Transaction。。区别是??
新问题是改了上面的以后,点提交就停止在“http://127.0.0.1:8080/webtest/addAdmin.do?name=mpmony&password=111111”上面。。
检查了FORWARDS,确认指向了SUCCESS,在AddAdminAction.execute返回的值也是SUCCESS。
session.getTransaction()获得一个事务,前提是当前session中已经有了事务后面那个问题应该是struts没有成功跳转,不知道你的HibernateSessionFactory类是怎么写的
你还是把你的Action、struts-config.xml都贴出来吧,帮你看看~~
package com;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory { /**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* 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();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}}
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.yourcompany.Admin;
import com.yourcompany.AdminDAOFactory;
import com.yourcompany.AdminId;
import com.yourcompany.struts.form.AddAdminForm;/**
* MyEclipse Struts
* Creation date: 07-04-2007
*
* XDoclet definition:
* @struts.action path="/addAdmin" name="addAdminForm" input="/form/addAdmin.jsp" scope="request" validate="true"
*/
public class AddAdminAction extends Action {
/*
* Generated Methods
*/ /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AddAdminForm addAdminForm = (AddAdminForm) form;// TODO Auto-generated method stub
AdminId adid = new AdminId();
adid.setName(addAdminForm.getName());
adid.setPassword(addAdminForm.getPassword());
Admin admin = new Admin(adid);
AdminDAOFactory adminDAO = new AdminDAOFactory();
adminDAO.add(admin);
return mapping.findForward("success");
}
}
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans >
<form-bean name="addAdminForm" type="com.yourcompany.struts.form.AddAdminForm" />
<form-bean name="successForm" type="com.yourcompany.struts.form.SuccessForm" /> </form-beans> <global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="addAdminForm"
input="/form/addAdmin.jsp"
name="addAdminForm"
path="/addAdmin"
scope="request"
type="com.yourcompany.struts.action.AddAdminAction">
<forward
name="success"
path="/form/success.jsp"
redirect="true" />
</action>
</action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
if (it.hasNext()) {
errors.add("addAdmin.err.name", new ActionMessage("form.addAdmin.err.name"));
}在if里面加上it.next();否则就陷入死循环了