-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Error "save is not valid without active transaction&quo
PostPosted: Sat Jan 28, 2006 2:42 pm 
Newbie

Joined: Sat Jan 28, 2006 1:49 pm
Posts: 3
Hibernate version 3.1, Dec 12, 2005
Java version "1.4.2_10"
App Server JRUN 4.0

I have an EJB with method in it and a DAO with 1 method also. When the DAO calls session.save() it throws an exception "save is not valid without active transaction" (stacktrace further down).

1) Hibernate initialized using
static {
try {
sessionFactory = new Configuration().configure().
buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
2) Remote EJB has 1 method.
3) EJB method has a trans-attribute of "Required"
4) EJB calls DAO using this method
public void createEvent(Event event) {
EventDAOIF dao = new EventDAOImpl();
dao.create(event);
}
5) DAO Code is
public void create(Event event) {
Session session = HibernateUtil.getSessionFactory().
getCurrentSession();
session.save(event);
}

Mapping documents:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">com.int.datasource</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JRun4TransactionManagerLookup</property>
<property name="hibernate.session_factory_name">SessionFactory</property>
<property name="hibernate.current_session_context_class">jta</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<mapping resource="com/iw/integrate/data/event/Event.hbm.xml"/>
<mapping resource="com/iw/integrate/data/person/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.save(event);

Chapter 11. Transactions And Concurrency: Says the above is enough when using CMT

Full stack trace of any exception that occurs:
01/28 13:23:05 error save is not valid without active transaction
org.hibernate.HibernateException: save is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:289)
at $Proxy6.save(Unknown Source)
at com.iw.integrate.data.event.EventDAOImpl.create(EventDAOImpl.java:14)
at com.iw.integrate.business.event.EventFacadeBean.createEvent(EventFacadeBean.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at jrun.ejb.interceptors.InvokerInterceptor.invokeObjectMethod(InvokerInterceptor.java:153)
at jrun.ejb.interceptors.ContainerManagedTransactionInterceptor.invokeRequired(ContainerManagedTransactionInterceptor.java:304)
at jrun.ejb.interceptors.ContainerManagedTransactionInterceptor.invokeTransactedMethod(ContainerManagedTransactionInterceptor.java:140)
at jrun.ejb.interceptors.TransactionInterceptor.invokeObjectMethod(TransactionInterceptor.java:130)
at jrun.ejb.interceptors.StatelessSessionInstanceInterceptor.invokeObjectMethod(StatelessSessionInstanceInterceptor.java:49)
at jrun.ejb.interceptors.EJBSecurityInterceptor.invokeObjectMethod(EJBSecurityInterceptor.java:81)
at jrun.ejb.EJBContainer.invokeObjectMethod(EJBContainer.java:193)
at jrun.ejb.invocation.RemoteInvocationProxyImpl.invoke(RemoteInvocationProxyImpl.java:109)
at jrunx.rmi.RMIBroker.invokeEJB(RMIBroker.java:235)
at jrunx.rmi.RMIBroker.invoke(RMIBroker.java:142)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

01/28 13:23:14 info The container test-ejb#com.iw.integrate.business.event.EventAPI rolled back a transaction it began for the method createEvent.
01/28 13:23:14 error save is not valid without active transaction
javax.ejb.EJBException: save is not valid without active transaction
at jrun.ejb.interceptors.InvokerInterceptor.handleThrowable(InvokerInterceptor.java:75)
at jrun.ejb.interceptors.InvokerInterceptor.invokeObjectMethod(InvokerInterceptor.java:159)
at jrun.ejb.interceptors.ContainerManagedTransactionInterceptor.invokeRequired(ContainerManagedTransactionInterceptor.java:304)
at jrun.ejb.interceptors.ContainerManagedTransactionInterceptor.invokeTransactedMethod(ContainerManagedTransactionInterceptor.java:140)
at jrun.ejb.interceptors.TransactionInterceptor.invokeObjectMethod(TransactionInterceptor.java:130)
at jrun.ejb.interceptors.StatelessSessionInstanceInterceptor.invokeObjectMethod(StatelessSessionInstanceInterceptor.java:49)
at jrun.ejb.interceptors.EJBSecurityInterceptor.invokeObjectMethod(EJBSecurityInterceptor.java:81)
at jrun.ejb.EJBContainer.invokeObjectMethod(EJBContainer.java:193)
at jrun.ejb.invocation.RemoteInvocationProxyImpl.invoke(RemoteInvocationProxyImpl.java:109)
at jrunx.rmi.RMIBroker.invokeEJB(RMIBroker.java:235)
at jrunx.rmi.RMIBroker.invoke(RMIBroker.java:142)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Name and version of the database you are using:
Database Microsoft SQLServer 2000

The generated SQL (show_sql=true):
YES

Debug level Hibernate log excerpt:
Server test ready (startup time: 25 seconds)
- Hibernate 3.1
- hibernate.properties not found
- using CGLIB reflection optimizer
- using JDK 1.4 java.sql.Timestamp handling
- configuring from resource: /hibernate.cfg.xml
- Configuration resource: /hibernate.cfg.xml
- Reading mappings from resource: com/iw/integrate/data/event/Event.hbm.xml
- Mapping class: com.iw.integrate.data.event.Event -> EVENTS
- Mapping collection: com.iw.integrate.data.event.Event.participants -> PERSON_EVENT
- Reading mappings from resource: com/iw/integrate/data/person/Person.hbm.xml
- Mapping class: com.iw.integrate.data.person.Person -> PERSON
- Mapping collection: com.iw.integrate.data.person.Person.events -> PERSON_EVENT
- Mapping collection: com.iw.integrate.data.person.Person.emailAddresses -> PERSON_EMAIL_ADDR
- Configured SessionFactory: null
- processing extends queue
- processing collection mappings
- processing association property references
- processing foreign key constraints
- JNDI InitialContext properties:{}
- Using datasource: com.int.datasource
- RDBMS: Microsoft SQL Server, version: 8.0.194
- JDBC driver: Microsoft SQL Server 2005 JDBC Driver - Beta 1, version: 1.0.107.104
- Using dialect: org.hibernate.dialect.SQLServerDialect
- Transaction strategy: org.hibernate.transaction.CMTTransactionFactory
- instantiating TransactionManagerLookup: org.hibernate.transaction.JRun4TransactionManagerLookup
- instantiated TransactionManagerLookup
- Automatic flush during beforeCompletion(): disabled
- Automatic session close at end of transaction: disabled
- Scrollable result sets: enabled
- JDBC3 getGeneratedKeys(): enabled
- Connection release mode: auto
- Default batch fetch size: 1
- Generate SQL with comments: disabled
- Order SQL updates by primary key: disabled
- Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
- Using ASTQueryTranslatorFactory
- Query language substitutions: {}
- Second-level cache: enabled
- Query cache: disabled
- Cache provider: org.hibernate.cache.NoCacheProvider
- Optimize cache for minimal puts: disabled
- Structured second-level cache entries: disabled
- Echoing all SQL to stdout
- Statistics: disabled
- Deleted entity synthetic identifier rollback: disabled
- Default entity-mode: pojo
- building session factory
- Factory name: SessionFactory
- JNDI InitialContext properties:{}
- Bound factory to JNDI name: SessionFactory
- InitialContext did not implement EventContext
- JNDI InitialContext properties:{}
- Checking 0 named HQL queries
- Checking 0 named SQL queries

I have spent 2 days reading up the relevant tutorials on hibernate, searching over the web for that particular error message and generally EJB-Hibernate interaction and I was unable to find any solution to my problem. I also tried "How do I use Hibernate in an EJB 2.1 session bean?" in Miscellaneous FAQs. I also read "How To Ask For Help" twice just in case I missed something obvious. So please believe me when I say I tried several avenues before posting. Any help is appreciated.

Thanks
-- Raag


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 28, 2006 4:05 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
what about
session.beginTransaction();

session.getTransaction().commit();

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 29, 2006 2:11 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Code:
<property name="current_session_context_class">thread</property>

should be
Code:
<property name="current_session_context_class">jta</property>


or as mentioned in the JavaDocs for ThreadLocalSessionContext,
Quote:
...
In order for ensuring that happens, the sessions generated here are unusable until after Session.beginTransaction() has been called.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 1:26 pm 
Newbie

Joined: Sat Jan 28, 2006 1:49 pm
Posts: 3
rajasaur :
If you look at my post, you will see that that is what I have set the property to <property name="current_session_context_class">jta</property>

LaLiLuna ::
Maybe my understanding is incorrect, but is not the EJB responsible for starting and commiting transaction since I set the attribute to "Required" in ejb-jar.xml ? I was also under the impression that the Session will atomatically associate itself wth the transaction created by the container when a call is made to a method to the EJB.

If you look at Section "11.2.2. Using JTA" in the online tutorial

http://www.hibernate.org/hib_docs/v3/re ... tions.html

It clearly shows that in the case of CMT, I should not have to call openTransaction()

Is my understanding incorrect ?

Thanks in advance
-- Raag


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 1:28 pm 
Newbie

Joined: Sat Jan 28, 2006 1:49 pm
Posts: 3
rajasaur ::
My apologies.

There is a typo in my original post. The line with "Thread" is not in my configuration file. I must have pasted it accidently.

-- Raag


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 1:30 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
You have hibernate.current_session_context_class set to jta and
current_session_context_class set to "thread". Im not sure if the earlier property(hibernate.current_session_context_class) is even considered. You need to change the value of current_session_Context_class from "thread" to "jta".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 10:26 pm 
Regular
Regular

Joined: Wed Apr 12, 2006 12:49 am
Posts: 105
Location: Malaysia
Hi,

What is the difference between using "thread" and "jta"?

Code:
<property name="hibernate.current_session_context_class">thread</property>


and

Code:
<property name="hibernate.current_session_context_class">jta</property>


Thank you.

_________________
Thank you.

Regards,
Jap.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.