Announcement Announcement Module
Collapse
No announcement yet.
Transaction Problem Spring 3.0.2 + Glassfish 3 + JPA 2.0 (EclipseLink) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction Problem Spring 3.0.2 + Glassfish 3 + JPA 2.0 (EclipseLink)

    I'm getting this error while executing insert operation in the database
    Code:
    javax.persistence.TransactionRequiredException: 
    Exception Description: No externally managed transaction is currently active for this thread
    The select operation works well but i can't persist in the database..following are my configurations

    Persistence.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
      <persistence-unit name="JpaTest3PU" transaction-type="JTA">
        <jta-data-source>jndi.test3</jta-data-source>
        <class>riaj.test.Contact</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
          <property name="eclipse.server-platform" value="SunAS9"/>
        </properties>
      </persistence-unit>
    </persistence>
    Application-context.xml

    Code:
    <?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:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
        
        
        <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
            <property name="persistenceUnitName" value="JpaTest3PU" />
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" >
                    <property name="showSql" value="true" />
                    <property name="generateDdl" value="false" />
                    <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform" />
                </bean>
            </property>
            <property name="loadTimeWeaver">
                <bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
            </property>
        </bean>
      
        <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/contact" />
            <property name="username" value="root" />
            <property name="password" value="123456" />
        </bean>
      
       
        <tx:annotation-driven transaction-manager="txManager" />
        <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean> 
    </beans>
    My Entity Class Contact.java

    Code:
    package riaj.test;
    
    import java.io.Serializable;
    import javax.persistence.Basic;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.Table;
    
    /**
     *
     * @author riaj
     */
    @Entity
    @Table(name = "contact")
    @NamedQueries({
        @NamedQuery(name = "Contact.findAll", query = "SELECT c FROM Contact c"),
        @NamedQuery(name = "Contact.findById", query = "SELECT c FROM Contact c WHERE c.id = :id"),
        @NamedQuery(name = "Contact.findByLastname", query = "SELECT c FROM Contact c WHERE c.lastname = :lastname"),
        @NamedQuery(name = "Contact.findByFirstname", query = "SELECT c FROM Contact c WHERE c.firstname = :firstname")})
    public class Contact implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "id")
        private Integer id;
        @Column(name = "lastname")
        private String lastname;
        @Column(name = "firstname")
        private String firstname;
    
        public Contact() {
        }
    
        public Contact(String fname, String lname) {
            this.firstname = fname;
            this.lastname = lname;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getLastname() {
            return lastname;
        }
    
        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
    
        public String getFirstname() {
            return firstname;
        }
    
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
    
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
    
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Contact)) {
                return false;
            }
            Contact other = (Contact) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
    
        @Override
        public String toString() {
            return "riaj.test.Contact[id=" + id + "]";
        }
    
    }
    the class where the persist operation is -

    Code:
    package riaj.test;
    
    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.TypedQuery;
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    /**
     *
     * @author riaj
     */
    @Repository
    @Transactional
    public class ContactImpl implements ContactInterface {
    
        @PersistenceContext
        private EntityManager em;
    
        @Override
        public List<Contact> getAllContacts() {
            TypedQuery<Contact> query = em.createQuery("SELECT c FROM Contact c", Contact.class);
            return query.getResultList();
        }
    
        public void insert(Contact c) {
            em.persist(c);
            em.flush();
        }
    }
    Please tell me what am i doing wrong?

  • #2

    Please tell me what am i doing wrong?
    Your configuration is wrong.

    You tell the EntityManagerFactory that you want to use JTA but in your configuration you have a JpaTransactionManager which is for local transactions and not JTA transactions. Either switch to local transactions or configure your transaction manager correctly.

    Comment


    • #3
      Sorry i know very little about Spring could you please kindly tell me how should i configure the transaction manager for JTA?

      Comment

      Working...
      X