use Java Persistence with Hibernate Book example
I use Hibernate3.2 + MySql5.0+Struts1.2
org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
at com.depot.web.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:64)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.depot.web.filter.SetCharacterEncoding.doFilter(SetCharacterEncoding.java:67)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:833)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1285)
at java.lang.Thread.run(Thread.java:619)
configfile
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
<property name="connection.url">
jdbc:mysql://192.168.1.8:3306/depot
</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">depot</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- Use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- Print SQL to stdout, format it nicely -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="current_session_context_class">thread</property>
<!-- Entity Mapping Config init -->
<mapping resource="com/depot/model/Client.hbm.xml" />
</session-factory>
Session Manager Filter
public class HibernateSessionRequestFilter implements Filter {
private static Log log = LogFactory.getLog(HibernateSessionRequestFilter.class);
private SessionFactory sf;
public void doFilter(ServletRequest req,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
String openSession = request.getParameter("session");
if(openSession == null || !("openSession".equalsIgnoreCase(openSession))){
log.debug("Skip OpenSession Starting a database transaction..");
chain.doFilter(request, response);
} else{
try {
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
System.out.println("Starting a database transaction");
sf.getCurrentSession().beginTransaction();
// Call the next filter (continue request processing)
chain.doFilter(request, response);
// Commit and cleanup
System.out.println("Committing the database transaction");
HibernateSessionFactory.getSession().getTransaction().commit();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
} catch (StaleObjectStateException staleEx) {
log.error("This interceptor does not implement optimistic concurrency control!");
log.error("Your application will not work until you add compensation actions!");
// Rollback, close everything, possibly compensate for any permanent changes
// during the conversation, and finally restart business conversation. Maybe
// give the user of the application a chance to merge some of his work with
// fresh data... what you do here depends on your applications design.
throw staleEx;
} catch (Throwable ex) {
// Rollback only
ex.printStackTrace();
try {
if (sf.getCurrentSession().getTransaction().isActive()) {
log.debug("Trying to rollback database transaction after exception");
sf.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable rbEx) {
log.error("Could not rollback transaction after exception!", rbEx);
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
}
// finally{
// if(HibernateSessionFactory.getSession().isOpen()){
// HibernateSessionFactory.getSession().close();
// System.out.println("*********** close CurrentSession *************");
// }
//
// }
}
}
public void init(FilterConfig filterConfig) throws ServletException {
log.debug("enter Initializing filter HibernateSessionFactory ...");
sf = HibernateUtil.getSessionFactory();
}
public void destroy() {}
Action
public class ClientManageAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionMessages error = new ActionMessages();
//org.hibernate.classic.Session session = (org.hibernate.classic.Session)request.getSession().getAttribute(Constants.HIBERNATE_SESSION_KEY);
ClientDAO dao = DAOFactory.getInstance().getClientDAO();
List result = dao.findAllClient();
if (result == null) {
error.add("clientNull",new ActionMessage("client.result.null"));
saveMessages(request, error);
DAO Classs
public class ClientHibernateDAO extends GenericHibernateDAO<Client, Long> implements ClientDAO {
public List findAllClient() {
return getSession().createCriteria(Client.class).list();
}
}
}
request.setAttribute("result", result);
dao = null;
return mapping.findForward("clientManage");
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Struts ActionSerlvet init -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hibernateSessionRequestFilter</filter-name>
<filter-class>com.depot.web.filter.HibernateSessionRequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateSessionRequestFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
Why???
Thanks you!!