Announcement Announcement Module
Collapse
No announcement yet.
spring-flex integration no transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-flex integration no transaction

    I am getting this exception thrown when I try to delete an item called through BlazeDS:

    javax.persistence.TransactionRequiredException : no transaction is in progress

    I have configured it to use the annotations method, didn't work. Used JpaSupportDao, didn't work either. Tried a dozen different ways to wrap it with a transaction and nothing.

    The jpa configuration is here: (I had to chop off the schema defs to make it fit)

    Code:
    <beans>
      
        <context:component-scan base-package="com.test.model">
            <context:include-filter type="annotation"
                expression="org.springframework.stereotype.Repository"/>
        </context:component-scan>
      
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
        
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="punit"/>
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="database" value="MYSQL" />
                    <property name="showSql" value="true" />
                </bean>
            </property>
            <property name="jpaPropertyMap">
                <map>
                    <entry key="hibernate.hbm2ddl.auto" value="update" />
                </map>
            </property>
        </bean>
        
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
            
        </bean>
        
        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
        
        <jee:jndi-lookup id="dataSource" jndi-name="jdbc/TestDB" resource-ref="true"/>
    
    </beans>
    The service definition is this:

    Code:
    <beans>
        
         <context:component-scan base-package="com.test.service">
            <context:include-filter type="annotation"
                expression="org.springframework.stereotype.Service"/>
        </context:component-scan>
    
        <bean id="mySpringManagedMessageBroker" class="org.springframework.flex.messaging.MessageBrokerFactoryBean" /> 
        
        <!-- Maps request paths at /* to the BlazeDS MessageBroker -->
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <value>
                    /*=mySpringManagedMessageBroker
                </value>
            </property>
        </bean>
        
        <!-- Dispatches requests mapped to a MessageBroker -->
        <bean class="org.springframework.flex.messaging.servlet.MessageBrokerHandlerAdapter"/>    
    
        <!-- Expose the productService bean for BlazeDS remoting -->
        <bean id="product" class="org.springframework.flex.messaging.remoting.FlexRemotingServiceExporter">
            <property name="messageBroker" ref="mySpringManagedMessageBroker"/>
            <property name="service" ref="productService"/>
        </bean>
        
    </beans>
    The Service class is this:

    Code:
    package com.test.service;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.test.model.Product;
    
    @Service("productService")
    @Transactional
    public class ProductImpl {
    	
    	@PersistenceContext
        public EntityManager em;
    
    	@SuppressWarnings("unchecked")
    	public List<Product> findAll() {
    		return em.createNamedQuery(Product.FIND_ALL).getResultList();
    	}
    
    	public void deleteProduct(int id) {
    
    		try {
    		
    			Product prod = em.find(Product.class,id);
    		
    			em.remove(prod);
    		}
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    		em.flush();
    	}
    	
    	@SuppressWarnings("unchecked")
    	public List<Product> findNameAndQty() {
    		//return getJpaTemplate().find("Select new Product(p.name, p.qty) From Product p");
    		return null;
    	}
    	
    }
    and finally the Product object itself:

    Code:
    package com.test.model;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="product")
    @NamedQueries({
    	
    	@NamedQuery(name=Product.FIND_PRODUCT_BY_NAME, query="SELECT product from Product product where product.name = :name"),
    	@NamedQuery(name=Product.FIND_PRODUCT_BY_NAME_WITHOUT, query="SELECT product from Product product where product.name = ?1"),
    	@NamedQuery(name=Product.FIND_ALL, query="SELECT product from Product product")
    	
    })
    public class Product {
        
    	public static final String FIND_ALL = "findAllProducts";
    	public static final String FIND_PRODUCT_BY_NAME = "findProductByName";
    	public static final String FIND_PRODUCT_BY_NAME_WITHOUT = "findProductByNameWithout";
    	
    	@Id
    	@GeneratedValue
        private int productId;
    	@Column(name="product_name", length=20)
        private String name;
        private String description;
        private String image;
        private String category;
        private double price;
        private int qty;
        
        @OneToMany(mappedBy="product")
        private List<Sku> skus = new ArrayList<Sku>();
        
        public Product() {
        	
        }
        
        public Product(String name, int qty) {
        	this.name = name;
        	this.qty = qty;
        }
        
        public Product(int productId, String name, String description, String image, String category, double price, int qty) {
    		this.productId = productId;
    		this.name = name;
    		this.description = description;
    		this.image = image;
    		this.category = category;
    		this.price = price;
    		this.qty = qty;
    	}
    
       ... truncated
    
    }
    What am I missing? The full stack trace is this:

    javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush( AbstractEntityManagerImpl.java:301)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.springframework.orm.jpa.ExtendedEntityManagerC reator$ExtendedEntityManagerInvocationHandler.invo ke(ExtendedEntityManagerCreator.java:358)
    at $Proxy42.flush(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.springframework.orm.jpa.SharedEntityManagerCre ator$SharedEntityManagerInvocationHandler.invoke(S haredEntityManagerCreator.java:198)
    at $Proxy42.flush(Unknown Source)
    at com.crengland.service.ProductImpl.deleteProduct(Pr oductImpl.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at flex.messaging.services.remoting.adapters.JavaAdap ter.invoke(JavaAdapter.java:421)
    at flex.messaging.services.RemotingService.serviceMes sage(RemotingService.java:183)
    at flex.messaging.MessageBroker.routeMessageToService (MessageBroker.java:1503)
    at flex.messaging.endpoints.AbstractEndpoint.serviceM essage(AbstractEndpoint.java:884)
    at flex.messaging.endpoints.amf.MessageBrokerFilter.i nvoke(MessageBrokerFilter.java:121)
    at flex.messaging.endpoints.amf.LegacyFilter.invoke(L egacyFilter.java:158)
    at flex.messaging.endpoints.amf.SessionFilter.invoke( SessionFilter.java:44)
    at flex.messaging.endpoints.amf.BatchProcessFilter.in voke(BatchProcessFilter.java:67)
    at flex.messaging.endpoints.amf.SerializationFilter.i nvoke(SerializationFilter.java:146)
    at flex.messaging.endpoints.BaseHTTPEndpoint.service( BaseHTTPEndpoint.java:278)
    at org.springframework.flex.messaging.servlet.Message BrokerHandlerAdapter.handle(MessageBrokerHandlerAd apter.java:79)
    at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:875)
    at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:807)
    at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:571)
    at org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:511)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

  • #2
    Move/Copy your tx:annotation-driven to the service context. It is a BeanFactoryPostProcessor and only operates on the context it is defined in. Currently it only advises your repository beans not your services (which should be the layer transactions are defined on). Basically currently your @Transactional annotation on your service is useless.

    Comment


    • #3
      That was exactly it. Thank you Marten. I ended up moving it to my jpaContext file and am going to configure it through annotations there.

      Thanks again.

      Comment

      Working...
      X