-->
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.  [ 4 posts ] 
Author Message
 Post subject: Transaction is required for EntityManager.refresh()?
PostPosted: Tue Jul 05, 2005 2:48 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I'm using the EntityManager in a J2SE environment. Using Hibernate with a Session rather than an EntityManager, I've been able to Session.refresh() an object without having to have it wrapped in a transaction. However, I found that when using EntityManager.refresh() I do need a transaction? This confuses me a little, as I would expect refresh() to be a read-only operation. Normally, I usually just wrap operations that write in a transaction. I'm wondering if I've been doing things wrong and that I should start my transactions earlier, if this is a bug, or, if I'm doing something else wrong?

Hibernate version: hibernate-3.1alpha1, hibernate-annotations-3.1beta3, hibernate-entitymanager-3.1beta1

Code between sessionFactory.openSession() and session.close():

Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("bb_bb60");
EntityManager em = emf.createEntityManager();
CourseRole cr = em.find(CourseRole.class, "S");
System.out.println(cr);
em.close();
em = emf.createEntityManager();
//EntityTransaction et = em.getTransaction();
//et.begin();
em.refresh(cr);
System.out.println(cr);


Full stack trace of any exception that occurs:

[exec] javax.persistence.TransactionRequiredException: no transaction is in progress
[exec] at org.hibernate.ejb.AbstractEntityManagerImpl.checkTransactionActive(AbstractEntityManagerImpl.java:122)
[exec] [S, Student]
[exec] at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:157)
[exec] at edu.uchicago.at.blackboard.test.Test.main(Test.java:50)

Name and version of the database you are using: Oracle 9i

Debug level Hibernate log excerpt:

[exec] Jul 5, 2005 1:36:00 PM org.hibernate.cfg.Environment <clinit>
[exec] INFO: Hibernate 3.1alpha1
[exec] Jul 5, 2005 1:36:00 PM org.hibernate.cfg.Environment <clinit>
[exec] INFO: hibernate.properties not found
[exec] Jul 5, 2005 1:36:00 PM org.hibernate.cfg.Environment <clinit>
[exec] INFO: using CGLIB reflection optimizer
[exec] Jul 5, 2005 1:36:00 PM org.hibernate.cfg.Environment <clinit>
[exec] INFO: using JDK 1.4 java.sql.Timestamp handling
[exec] Jul 5, 2005 1:36:02 PM org.hibernate.cfg.Configuration secondPassCom
pile
[exec] INFO: processing extends queue
[exec] Jul 5, 2005 1:36:02 PM org.hibernate.cfg.Configuration secondPassCom
pile
[exec] INFO: processing collection mappings
[exec] Jul 5, 2005 1:36:02 PM org.hibernate.cfg.annotations.CollectionBinde
r bindCollectionSecondPass
[exec] INFO: Mapping collection: edu.uchicago.at.blackboard.persistence.ent
ity.Course.courseMemberships -> course_users
[exec] Jul 5, 2005 1:36:02 PM org.hibernate.cfg.annotations.CollectionBinde
r bindCollectionSecondPass
[exec] INFO: Mapping collection: edu.uchicago.at.blackboard.persistence.ent
ity.User.calendarItems -> calendar
[exec] Jul 5, 2005 1:36:02 PM org.hibernate.cfg.Configuration secondPassCom
pile
[exec] INFO: processing association property references
[exec] Jul 5, 2005 1:36:02 PM org.hibernate.cfg.Configuration secondPassCom
pile
[exec] INFO: processing foreign key constraints
[exec] Jul 5, 2005 1:36:03 PM org.hibernate.cfg.Configuration secondPassCom
pile
[exec] INFO: processing extends queue
[exec] Jul 5, 2005 1:36:03 PM org.hibernate.cfg.Configuration secondPassCom
pile
[exec] INFO: processing collection mappings
[exec] Jul 5, 2005 1:36:03 PM org.hibernate.cfg.Configuration secondPassCom
pile
[exec] INFO: processing association property references
[exec] Jul 5, 2005 1:36:03 PM org.hibernate.cfg.Configuration secondPassCom
pile
[exec] INFO: processing foreign key constraints
[exec] Jul 5, 2005 1:36:03 PM org.hibernate.connection.DriverManagerConnect
ionProvider configure
[exec] INFO: Using Hibernate built-in connection pool (not for production u
se!)
[exec] Jul 5, 2005 1:36:03 PM org.hibernate.connection.DriverManagerConnect
ionProvider configure
[exec] INFO: Hibernate connection pool size: 20
[exec] Jul 5, 2005 1:36:03 PM org.hibernate.connection.DriverManagerConnect
ionProvider configure
[exec] INFO: autocommit mode: true
[exec] Jul 5, 2005 1:36:03 PM org.hibernate.connection.DriverManagerConnect
ionProvider configure
[exec] INFO: using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:ora
cle:thin:@localhost:1522:BB60
[exec] Jul 5, 2005 1:36:03 PM org.hibernate.connection.DriverManagerConnect
ionProvider configure
[exec] INFO: connection properties: {user=appointments, password=****, auto
commit=true, release_mode=auto}
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.
2.0.6.0 - 64bit Production
[exec] With the Partitioning, OLAP and Oracle Data Mining options
[exec] JServer Release 9.2.0.6.0 - Production
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: JDBC driver: Oracle JDBC driver, version: 9.2.0.5.0
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.dialect.Dialect <init>
[exec] INFO: Using dialect: org.hibernate.dialect.Oracle9Dialect
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.transaction.TransactionFactoryF
actory buildTransactionFactory
[exec] INFO: Using default transaction strategy (direct JDBC transactions)
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.transaction.TransactionManagerL
ookupFactory getTransactionManagerLookup
[exec] INFO: No TransactionManagerLookup configured (in JTA environment, us
e of read-write or transactional second-level cache is not recommended)
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Automatic flush during beforeCompletion(): disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Automatic session close at end of transaction: disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: JDBC batch size: 15
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: JDBC batch updates for versioned data: disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Scrollable result sets: enabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: JDBC3 getGeneratedKeys(): disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Connection release mode: auto
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Default batch fetch size: 1
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Generate SQL with comments: disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Order SQL updates by primary key: disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory createQuery
TranslatorFactory
[exec] INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFact
ory
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.hql.ast.ASTQueryTranslatorFacto
ry <init>
[exec] INFO: Using ASTQueryTranslatorFactory
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Query language substitutions: {}
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Second-level cache: enabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Query cache: disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory createCache
Provider
[exec] INFO: Cache provider: org.hibernate.cache.EhCacheProvider
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Optimize cache for minimal puts: disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Structured second-level cache entries: disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Statistics: disabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Deleted entity synthetic identifier rollback: enabled
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.cfg.SettingsFactory buildSettin
gs
[exec] INFO: Default entity-mode: pojo
[exec] Jul 5, 2005 1:36:05 PM org.hibernate.impl.SessionFactoryImpl <init>
[exec] INFO: building session factory
[exec] Jul 5, 2005 1:36:05 PM net.sf.ehcache.config.Configurator configure
[exec] WARNING: No configuration found. Configuring ehcache from ehcache-fa
ilsafe.xml found in the classpath: jar:file:/opt/jre1.5.0_04-dev/lib/ext/ehcache
-1.1.jar!/ehcache-failsafe.xml
[exec] Jul 5, 2005 1:36:07 PM org.hibernate.impl.SessionFactoryObjectFactor
y addInstance
[exec] INFO: Not binding factory to JNDI, no JNDI name configured
[exec] Jul 5, 2005 1:36:07 PM org.hibernate.impl.SessionFactoryImpl checkNa
medQueries
[exec] INFO: Checking 0 named queries
[exec] Jul 5, 2005 1:36:07 PM org.hibernate.jdbc.AbstractBatcher log
[exec] FINE: select courserole0_.course_role as course1_0_, courserole0_.na
me as name3_0_ from bb_bb60.course_roles courserole0_ where courserole0_.course_
role=?
[exec] javax.persistence.TransactionRequiredException: no transaction is in
progress
[exec] at org.hibernate.ejb.AbstractEntityManagerImpl.checkTransactionA
ctive(AbstractEntityManagerImpl.java:122)
[exec] [S, Student]
[exec] at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractE
ntityManagerImpl.java:157)
[exec] at edu.uchicago.at.blackboard.test.Test.main(Test.java:50)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 3:32 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is as per the spec and for your own good.
Using tx, only for writing operation is as silly as using the safety belt only when driving and not being driven.
To be short, read operations has to be consistent as well (hence ACID), it is not faster to avoid tx, since your driver will have to do it for you under the cover.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 5:17 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I'm using the Open Session in View design pattern. Would you recommend that I simply begin the transaction immediately after I create the EntityManager and then commit it immediately before closing it (i.e. do this in the servlet filter)?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 9:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Check the caveat emptor sample app. There is an implementation of the HibernateUtil class that should fit your needs. Basically, it does late session & tx opening.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.