Hi, Please help me resolve the following. I am trying to use the CMT transaction to execute the Hibernate queries, but i am getting the following exception.
The Hibernate version I am using is Hibernate Version 3.2.4 GA which comes along with jboss-soa-p.4.3.0.
Code:
[b]Exception Stack Trace[/b]
16:18:30,681 ERROR [STDERR] org.hibernate.HibernateException: Unable to locate current JTA transaction
at org.hibernate.context.JTASessionContext.currentSession(JTASessionContext.java:61)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
at com.sample.new.services.data.dao.GenericHibernateDAO.<init>(GenericHibernateDAO.java:37)
at com.sample.new.services.data.dao.ConsumerDAO.<init>(ConsumerDAO.java:5)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at java.lang.Class.newInstance0(Class.java:350)
at java.lang.Class.newInstance(Class.java:303)
at com.sample.new.services.data.daofactory.HibernateDAOFactory.instantiateDAO(HibernateDAOFactory.java:16)
at com.sample.new.services.data.daofactory.HibernateDAOFactory.getConsumerDAO(HibernateDAOFactory.java:11)
at com.sample.new.ejb.consumer.ConsumerBO.updateLoginCredentials(ConsumerBO.java:12)
at com.sample.new.ejb.consumer.ConsumerBean.getCountry(ConsumerBean.java:17)
at org.apache.jsp.forgotPassword_jsp._jspService(forgotPassword_jsp.java:75)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
[b]My EJB[/b]
@Stateless
@TransactionManagement(value=TransactionManagementType.CONTAINER)
public class ConsumerBean implements ConsumerBeanLocal{
@javax.ejb.TransactionAttribute(TransactionAttributeType.REQUIRED)
public Boolean getCountry(CountryDO countryDO) throws Exception{
ConsumerBO consumerBO = new ConsumerBO();
consumerBO.updateLoginCredentials(countryDO);
return true;
}
}
[b]Hibernate Config XML[/b]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">xxxxx</property>
<property name="hibernate.connection.password">yyyyy</property>
<property name="hibernate.default_schema">zzzzz</property>
<property name="hibernate.connection.url">jdbc:mysql://10.66.166.139:3306/zzzzz</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.JbossTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/sample/new/services/data/hbms/Country.hbm.xml"/>
</session-factory>
</hibernate-configuration>
[b]GenericHibernateDAO constructor[/b]
public GenericHibernateDAO() {
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
} catch (HibernateException hExp) {
hExp.printStackTrace();
throw hExp;
}
}
[b]My Hibernate Util Class[/b]
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
1) In the above code "HibernateUtil.getSessionFactory().getCurrentSession();" the session is null.