1

当执行到达时,我遇到了事务问题

taskExecutor.execute();

我得到一个例外:

原因:org.springframework.transaction.IllegalTransactionStateException:没有为标有传播“强制”的事务找到现有事务

我知道这个错误是不言自明的,但我无法让它正常工作。

这就是我获取控制器并拨打电话的方式:

ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");
BeanFactory beanFactory = context;
FacadeControler f = beanFactory.getBean("facadeController");
f.method(reservation);

这是我的FacadeController

package xxx;

import ...

@Service
public class FacadeControllerImpl implements FacadeController {

    static Logger logger = Logger.getLogger(FacadeControllerImpl.class);


    @Qualifier("executor")
    @Autowired
    private TaskExecutor taskExecutor;


    @Transactional(propagation=Propagation.REQUIRED)
    public Reservation method(Reservation reservationFromEndpoint){

    ...
    taskExecutor.execute();
    ...

    }

这是Executor

@Component("executor")
public class QueueTaskExecutor implements TaskExecutor {

  final static Logger logger = LoggerFactory.getLogger(QueueTaskExecutor.class);


  @Autowired
  protected QueuedTaskHolderDao queuedTaskDao;

  @Autowired
  protected Serializer serializer;


  @Override
  @Transactional(propagation=Propagation.MANDATORY) 
  public void execute(Runnable task) {

      logger.debug("Trying to enqueue: {}", task);

      AbstractBaseTask abt; 
      try {
          abt = AbstractBaseTask.class.cast(task);
      } catch (ClassCastException e) {
          logger.error("Only runnables that extends AbstractBaseTask are accepted.");
          throw new IllegalArgumentException("Invalid task: " + task);
      }

      // Serialize the task
      QueuedTaskHolder newTask = new QueuedTaskHolder();
      byte[] serializedTask = this.serializer.serializeObject(abt);
      newTask.setTriggerStamp(abt.getTriggerStamp());

      logger.debug("New serialized task takes {} bytes", serializedTask.length);

      newTask.setSerializedTask(serializedTask);

      // Store it in the db
      this.queuedTaskDao.persist(newTask);

      // POST: Task has been enqueued
  } 

}

这是我的applicationContext.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:task="http://www.springframework.org/schema/task"
   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.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/task 
 http://www.springframework.org/schema/task/spring-task-3.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- Where to look for Spring components -->
<context:annotation-config />    
<context:component-scan base-package="com.xxx"/>

<!-- @Configurable with AspectJ -->
<context:spring-configured/>

<!-- A task scheduler that will call @Scheduled methods -->
<!--     <task:scheduler id="myScheduler" pool-size="10"/> -->
<!--     <task:annotation-driven scheduler="myScheduler"/> -->

<!-- DataSource -->
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="myDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/bbdd"/>
    <property name="username" value="user"/>
    <property name="password" value="pass"/>
</bean>

<!-- JPA Entity Manager -->
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="myEntityManagerFactory">
    <property name="dataSource" ref="myDataSource" />
    <property name="jpaVendorAdapter">
             <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"></bean>
    </property> 
    <property name="persistenceUnitName" value="comm_layer" />
    <property name="jpaPropertyMap">
        <map>
            <entry key="eclipselink.weaving" value="false"/>

            <entry key="eclipselink.ddl-generation" value="create-or-extend-tables"/>   
            <entry key="eclipselink.logging.level" value="INFO"/>   
        </map>
    </property>
</bean>

 <!-- Transaction management -->
<tx:annotation-driven mode="aspectj" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    </property>
    <property name="entityManagerFactory" ref="myEntityManagerFactory"/>
</bean>

<bean id="facadeController" class="xxx.FacadeControllerImpl">

4

0 回答 0