Results 1 to 10 of 10

Thread: javax.persistence.TransactionRequiredException: no transaction is in progress

  1. #1
    Join Date
    Sep 2009
    Posts
    21

    Default javax.persistence.TransactionRequiredException: no transaction is in progress

    I used Spring roo 1.00 to create a domain class, foo.domain.Profile, generated its integration test and the testMarkerMethod() everything works sweet.

    After I pushed in all the persistence methods into the domain class, developed an DAO e.g. foo.api.FooImpl that make use of the domain class's foo.domain.Profile's persist() indirectly to save the object into the data store but it does not seem to commit. Further investigation showed that there is no transaction created. The applicationContext.xml is as the following -

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schem...ng-aop-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schem...ing-tx-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schem...ontext-2.5.xsd
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

    <!-- Hack for JBoss until full compliance is reached -->
    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityMana gerFactoryBean">
    <property name="persistenceUnitName" value="MySQL_Local" />
    </bean>

    <!--
    Configure the Spring framework to use JTA transactions from Atomikos
    -->
    <bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTran sactionManager">
    <property name="transactionManager" ref="atomikosTransactionManager" />
    <property name="userTransaction" ref="atomikosUserTransaction" />
    <property name="transactionSynchronizationName" value="SYNCHRONIZATION_ON_ACTUAL_TRANSACTION" />
    </bean>

    <!-- Define domain classes that uses the EMF -->
    <bean id="profile" class="foo.domain.Profile">
    <property name="emf" ref="entityManagerFactory" />
    </bean>
    <!-- This is a api object that we want to make transactional.
    -->
    <bean id="fooImpl" class="foo.api.FooImpl" />

    <tx:annotation-driven/>
    <context:component-scan annotation-config="true" base-package="foo.domain"/>
    <context:component-scan annotation-config="true" base-package="foo.api"/>

    <!--
    ************************************************** ******************
    -->
    <!-- PropertyConfigurer for the DAO -->
    <!--
    ************************************************** ******************
    -->
    <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="location" value="classpath:database.properties" />
    </bean>

    <!--
    ************************************************** ******************
    -->
    <!-- Setup the transaction manager -->
    <!--
    ************************************************** ******************
    -->
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionMana ger"
    init-method="init" destroy-method="close">
    <property name="forceShutdown" value="true" />
    <property name="startupTransactionService" value="true" />
    <property name="transactionTimeout" value="60" />
    </bean>

    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" />


    <!--
    ************************************************** ******************
    -->
    <!-- A distinct entry for each persistence unit used in this project -->
    <!--
    ************************************************** ******************
    -->

    <!-- Make a Datasource -->
    <bean id="MySQL_LocalDS" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSo urceBean">
    <property name="uniqueResourceName" value="MySQL_Local__d3LUsKoLEd6rXPx8MV95xA" />
    <property name="driverClassName" value="${MySQL_Local.connection.driver_class}" />
    <property name="user" value="${MySQL_Local.connection.username}" />
    <property name="password" value="${MySQL_Local.connection.password}" />
    <property name="url" value="${MySQL_Local.connection.url}" />
    <property name="minPoolSize" value="${MySQL_Local.minPoolSize}" />
    <property name="maxPoolSize" value="${MySQL_Local.maxPoolSize}" />
    <!-- http://forum.springsource.org/showthread.php?t=67728 -->
    <property name="borrowConnectionTimeout" value="120" />
    <property name="testQuery" value="SELECT 1" />
    <property name="reapTimeout" value="0" />
    </bean>

    <!-- Configure a JPA vendor adapter -->
    <bean id="MySQL_LocalJPAVendorAdapter"
    class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <property name="showSql" value="${MySQL_Local.show_sql}" />
    <property name="generateDdl" value="${MySQL_Local.generateDdl}" />
    <property name="databasePlatform" value="${MySQL_Local.dialect}" />
    </bean>

    <!--
    now an EntityManager Factory that brings together the persistence
    unit, datasource, and JPA Vendor
    -->
    <bean id="MySQL_Local"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="MySQL_LocalDS" />
    <property name="persistenceUnitName" value="MySQL_Local" />
    <property name="jpaVendorAdapter" ref="MySQL_LocalJPAVendorAdapter" />
    <property name="jpaPropertyMap">
    <map>
    <entry key="hibernate.transaction.manager_lookup_class"
    value="com.atomikos.icatch.jta.hibernate3.Transact ionManagerLookup" />
    <entry key="hibernate.connection.release_mode" value="on_close" />
    </map>
    </property>
    </bean>

    </beans>

    (to continue as next reply ...)

  2. #2
    Join Date
    Sep 2009
    Posts
    21

    Default javax.persistence.TransactionRequiredException: no transaction is in progress

    (continued from the previous post due to 100000 limits)

    In persistence.xml -

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="MySQL_Local" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence </provider>
    <!-- JTA requires the container to provide the data source. Please set it up before launching the app, otherwise it won't work -->
    <jta-data-source>java:jdbc/MySqlDS</jta-data-source>

    <!-- Persistent classes -->
    <mapping-file>object_mappings.xml</mapping-file>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
    <property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.Transact ionManagerLookup"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.cache.use_second_level_cache" value="false"/>
    </properties>
    </persistence-unit>

    </persistence>

    The pushed-in domain class is -

    package foo.domain;

    import javax.persistence.Entity;
    import java.util.Date;

    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.OneToMany;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    import com.eolinc.programgoals.domain.PrgmGoalsRespondent ;
    import javax.persistence.ManyToOne;
    import javax.persistence.JoinColumn;

    @Entity
    public class Profile {

    private String profileType;

    private String profileFname;

    private String profileMname;

    private String profileLname;

    private String profileSuffx;

    private String profileAddr1;

    private String profileAddr2;

    private String profileAddr3;

    private String profileCity;

    ...

    @javax.persistence.Id
    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
    @javax.persistence.Column(name = "id")
    private java.lang.Long id;

    public String getProfileType() {
    return profileType;
    }

    public void setProfileType(String profileType) {
    this.profileType = profileType;
    }

    public String getProfileFname() {
    return profileFname;
    }

    public void setProfileFname(String profileFname) {
    this.profileFname = profileFname;
    }


    ....

    public static void setEntityManager(
    javax.persistence.EntityManager entityManager) {
    Profile.entityManager = entityManager;
    }

    @javax.persistence.Version
    @javax.persistence.Column(name = "version")
    private java.lang.Integer version;

    public java.lang.Long getId() {
    return this.id;
    }

    public void setId(java.lang.Long id) {
    this.id = id;
    }

    public java.lang.Integer getVersion() {
    return this.version;
    }

    public void setVersion(java.lang.Integer version) {
    this.version = version;
    }

    @org.springframework.transaction.annotation.Transa ctional
    public void persist() {
    if (this.entityManager == null) this.entityManager = entityManager();
    this.entityManager.persist(this);
    }

    @org.springframework.transaction.annotation.Transa ctional
    public void remove() {
    if (this.entityManager == null) this.entityManager = entityManager();
    if (this.entityManager.contains(this)) {
    this.entityManager.remove(this);
    } else {
    Profile attached = this.entityManager.find(Profile.class, this.id);
    this.entityManager.remove(attached);
    }
    }

    @org.springframework.transaction.annotation.Transa ctional
    public void flush() {
    if (this.entityManager == null) this.entityManager = entityManager();
    this.entityManager.flush();
    }

    @org.springframework.transaction.annotation.Transa ctional
    public void merge() {
    if (this.entityManager == null) this.entityManager = entityManager();
    Profile merged = this.entityManager.merge(this);
    this.entityManager.flush();
    this.id = merged.getId();
    }

    public static long countProfiles() {
    return (Long) entityManager().createQuery("select count(o) from Profile o").getSingleResult();
    }

    public static java.util.List<com.eolinc.programgoals.domain.Prof ile> findAllProfiles() {
    return entityManager().createQuery("select o from Profile o").getResultList();
    }

    public static com.eolinc.programgoals.domain.Profile findProfile(java.lang.Long id) {
    if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of Profile");
    return entityManager().find(Profile.class, id);
    }

    public static java.util.List<com.eolinc.programgoals.domain.Prof ile> findProfileEntries(int firstResult, int maxResults) {
    return entityManager().createQuery("select o from Profile o").setFirstResult(firstResult).setMaxResults(maxR esults).getResultList();
    }

    transient static private EntityManagerFactory emf;

    public EntityManagerFactory getEmf() {
    return emf;
    }

    public void setEmf(EntityManagerFactory emf) {
    this.emf = emf;
    }

    static private EntityManager entityManager() {
    return emf.createEntityManager();
    }

    transient static private javax.persistence.EntityManager entityManager;

    public static javax.persistence.EntityManager getEntityManager() {
    if (entityManager == null) entityManager = entityManager();
    return entityManager;
    }
    }

    In the DAO -

    package foo.api;

    import java.util.List;

    import foo.domain.Profile;
    import org.springframework.transaction.annotation.Isolati on;
    import org.springframework.transaction.annotation.Propaga tion;
    import org.springframework.transaction.annotation.Transac tional;
    import org.springframework.transaction.annotation.Isolati on;
    import org.springframework.transaction.annotation.Propaga tion;
    import org.springframework.transaction.annotation.Transac tional;
    @Transactional(
    propagation = Propagation.REQUIRES_NEW,
    isolation = Isolation.DEFAULT,
    readOnly = true)
    public class FooImpl implements FooInterface {

    ...

    @Transactional(readOnly = false)
    public void insertUpdate(Profile profile) {
    profile.persist();
    System.out.println("committing ...");
    profile.flush();
    }

    }

    and I got "javax.persistence.TransactionRequiredExceptio n: no transaction is in progress" exception when deployed on JBoss 5.1.0GA. Log snippet -

    ...
    16:00:53,329 ERROR [STDERR] javax.persistence.TransactionRequiredException: no t
    ransaction is in progress
    19:41:53,334 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.f
    lush(AbstractEntityManagerImpl.java:301)
    19:41:53,339 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(
    Native Method)
    19:41:53,345 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(N
    ativeMethodAccessorImpl.java:39)
    19:41:53,350 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invo
    ke(DelegatingMethodAccessorImpl.java:25)
    19:41:53,354 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:5
    97)
    19:41:53,358 ERROR [STDERR] at org.springframework.orm.jpa.ExtendedEntityMan
    agerCreator$ExtendedEntityManagerInvocationHandler .invoke(ExtendedEntityManagerC
    reator.java:365)
    19:41:53,362 ERROR [STDERR] at $Proxy297.flush(Unknown Source)
    ...

    Any idea what I miss in the configuration or codes? Why is no transaction created?

    Thanks for any insight!

  3. #3
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Roo transactions are implemented via AspectJ, specifically:

    Code:
       <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
    I see your application context has changed quite a bit from the original form Roo would have provided. As such this question has really become more of a Spring general usage question than a Roo one, as you don't appear to be using Roo any more. I would recommend you go back to the Roo default configuration. If there's some issues with Roo's default configuration, please let us know what these are and we can try to accommodate it in Roo itself.
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

  4. #4
    Join Date
    Sep 2009
    Posts
    21

    Default

    Thanks Ben. I will do so. one
    more question, are you saying that if
    i use annotation based transaction, it
    will not work as spring roo uses
    aspectj based transaction originally?

  5. #5
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    In theory it should work, as your annotation-driven element will setup Spring AOP-based proxy objects instead of relying on AspectJ compile-time-weaving. However, given the variance from the normal Roo project setup, it's possible there is some strange incompatibility present to do with AspectJ being applied (probably via the spring-aspects-*.jar aspect library) when your proxy object is being created. Again, I'd recommend you use the Roo structure by default as it is proven to work really nicely. If the Roo structure is somehow unsuitable, we'd like to understand why so that we can address this for you and the rest of the Roo community.
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

  6. #6
    Join Date
    Sep 2009
    Posts
    21

    Default

    I used the Spring Roo's applicationContext.xml and I got null pointer exception in the following codes -

    static private EntityManager entityManager() {
    return emf.createEntityManager();
    }

    Any idea what could be wrong?

    tks

  7. #7
    Join Date
    Sep 2009
    Posts
    21

    Default

    Ok, let me rephraze my question: will Spring Roo generated codes works with JBoss 5.1.0GA? With the exact applicationContext.xml and persistence.xml?

    If so, could someone who is guru in JBoss 5.1.0GA and Spring Roo 1.00 help me?

    Thanks.

  8. #8
    Join Date
    May 2009
    Location
    Vancouver
    Posts
    274

    Default

    Just to chime in on this.

    I have seen the original problem "no transaction is in progress" occur in some situations if using AspectJ 1.6.5 (or earlier). What are you using in your deployment scenario? If you are on 1.6.5 it is definetly worth trying 1.6.6 or 1.6.8.

    cheers
    Andy
    ---
    Andy Clement
    AspectJ Development

  9. #9
    Join Date
    Sep 2009
    Posts
    21

    Default

    Thanks Andy but we use 1.6.5, the version that Roo uses.

    This has been too long. Is there any kind soul out here that can send us the basic applicationContext.xml, persistence.xml and mySQL-DS.xml that works with JBoss 5.1.0.GA with Spring Roo 1.00?

    I think that is better and easier request. We will modify the files to point the database instance that we use to see if it works. Please supply DDL script also if required (if that makes it easier).

  10. #10
    Join Date
    Jan 2008
    Location
    Florence (IT)
    Posts
    10

    Default

    Hi all,

    I found a similar problem while I was testing aop function with jpa which use hibernate as engine for a mysql 5 database.

    In my dao implementation i inject the entityManagerFactory, and define a method

    Code:
    @PersistenceContext
    private EntityManagerFactory entityManagerFactory;
    
    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
    	this.entityManagerFactory = entityManagerFactory;
    }
    
    public EntityManagerFactory getEntityManagerFactory() {
    	return entityManagerFactory;
    }
    
    public EntityManager getEntityManager() {
    	getEntityManagerFactory.createEntityManager();
    }
    I make many test in configuration of this block, and i resolve the no transaction with this :

    Code:
    	
    	@PersistenceContext
    	private EntityManager entityManager;
    
    	public void setEntityManager(EntityManager entityManager) {
    		this.entityManager = entityManager;
    	}
    
    	public EntityManager getEntityManager() {
    		return entityManager;
    	}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •