简体   繁体   中英

TransactionRequiredException: No transactional EntityManager available within @Transactional method

Having designed Service method including @Transactional method, I faced the problem of "No transactional EntityManager available". Technogoly uses: JPA, Spring, Maven

Exception stackTrace

Exception in thread "main" javax.persistence.TransactionRequiredException: No transactional EntityManager available
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:273)
at com.sun.proxy.$Proxy29.merge(Unknown Source)
at com.restaurant.DAO.OrderDAOImpl.saveOrder(OrderDAOImpl.java:24)
at com.restaurant.services.DeliveryStaffServiceImpl.changeStatusOfOrder(DeliveryStaffServiceImpl.java:37)
at com.restaurant.entities.Main.main(Main.java:161)

The way I invoke service in main:

DeliveryStaffService del = context.getBean(DeliveryStaffService.class);
    List<Order> listOfOrders = del.getListOfOrders();

Trying to figure out the issue tried:

  1. to check versions of Spring in pom.xml;
  2. to check whether all @transactional methods are public;
  3. added in beans.xml
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

Moreover, there are a few services with @transactional methods int the same project, which work without any problems.

Here is my persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<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"
xmlns="http://java.sun.com/xml/ns/persistence"  version="2.0" >
    <persistence-unit name="Restaurant" transaction-type="RESOURCE_LOCAL">
        <provider>
             org.eclipse.persistence.jpa.PersistenceProvider
        </provider>
    <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:derby:..//RestaurantDB1;create=true" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
                 <property name="eclipselink.weaving" value="static" />
    </properties>
    </persistence-unit>
 </persistence> 

Beans.xml

 <?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.restaurant"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="com.restaurant"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitName" value="Restaurant"/>

        <property name="jpaDialect"> 
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/> 
        </property> 

        <property name="jpaVendorAdapter"> 
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                <property value="true" name="showSql"/> 
                <property value="true" name="generateDdl"/>
                <property name="databasePlatform" value="org.eclipse.persistence.platform.database.DerbyPlatform"/> 
            </bean> 
        </property>

        <property name="jpaPropertyMap">
            <map>
              <entry key="eclipselink.weaving" value="false" />
            </map>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
          <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
        <property name="url" value="jdbc:derby:..//RestaurantDB1;create=true" /> 
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/> 
    <tx:annotation-driven proxy-target-class="true"/> 
</beans>

Service Implementation

 @Named
public class KitchenStaffServiceImpl implements KitchenStaffService {

    @Inject
    private ItemDAO itemDAO;
    @Inject
    private OrderDAO orderDAO;

    @Override
    public List<Item> viewItems() {
        return itemDAO.viewItems();
    }

    @Override
    @Transactional
    public void processItem(Item item) {
        int i=item.getQuantity();
        while(i>0){
            try {
                Thread.sleep(1000);
                --i;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        item.setIsCooked(true);
        itemDAO.saveItem(item);
        System.out.println(item.getId());

        List<Item> list = item.getOrder().getListOfDishes();
        if(list.stream().allMatch(t -> t.getIsCooked()==true)){
            Order ord = item.getOrder();
            ord.setStatus(StatusOfDelivery.READY_FOR_SHIPMENT);
            orderDAO.saveOrder(ord);
        }
    }
}

DAO Implementation

    @Repository
public class ItemDAOImpl implements ItemDAO{

    @PersistenceContext
    private EntityManager em; 

    @Override
    public void saveItem(Item item){
        if(item.getId()==0){
            em.persist(item);
        }else
            em.merge(item);
    }       
}

OrderDAOImpelementation with the method saveOrder(Order order) is the same as in ItemDAO (persist()/merge())

Thanks in advance for any propositions

Your're calling

OrderDAOImpl.saveOrder();

from the method

DeliveryStaffServiceImpl.changeStatusOfOrder();

but in the example you show that @Transactional annotation is put on an unrelated method KitchenStaffServiceImpl.processItem() . Most likely, you need the @Transactional annotation on DeliveryStaffServicleImpl .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM