【问题标题】:No transactional EntityManager available even though one is configured即使配置了一个事务性EntityManager,也没有可用的事务性EntityManager
【发布时间】:2015-07-30 21:37:11
【问题描述】:

我正在尝试通过 JPA 和 Spring 数据与我的 MySQL 数据库进行事务交互。

配置

@Configuration
@EnableJpaRepositories("com.some.models.repositories")
@EnableTransactionManagement
public class MegabotsConfiguration {

    @Bean
    public EntityManagerFactory entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setJpaProperties(additionalProperties());
        factory.setPackagesToScan(new String[] { "com.datarank.megabots.models.entities", "com.datarank.megabots.models.repositories" });
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();

        return factory.getObject();
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/local_ttaggdb");
        dataSource.setUsername( "root" );
        dataSource.setPassword( "" );
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
        return new PersistenceExceptionTranslationPostProcessor();
    }

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        return properties;
    }
//...
}

这是我尝试使用事务的地方:

@Repository
@Scope("prototype")
@Transactional
public class BotWorkQueueRepository {

    @Autowired
    private BotWorkQueueJPARepository botWorkQueueJPARepository; //this is the jparepository referenced in the @EnableJpaRepositories in config

    @PersistenceContext
    private EntityManager entityManager;

    @Transactional
    public synchronized void flagInFlight(final List<BotWorkQueueEntity> botWorkQueueEntities) {
        try {
            //...set a few properties on entities, then
            entityManager.flush(); //Exception thrown here
            entityManager.clear();
        } catch (Exception e) {
            LOGGER.warn("Error making in flight: " + e);
        }
    }    
}

我得到的错误:

javax.persistence.TransactionRequiredException: No transactional EntityManager available

对于 SO 中发布的此错误,我已经尝试了大多数解决方案,但均未成功。

【问题讨论】:

    标签: spring jpa annotations


    【解决方案1】:

    一件事首先, 在 maven 中,如果您使用的是 appassembler-maven-plugin,请将以下行添加到 POM 文件中的 plugin.configuration 标记中。

    &lt;extraJvmArguments&gt;-javaagent:"$REPO"/org/aspectj/aspectjweaver/1.8.5/aspectjweaver-1.8.5.jar&lt;/extraJvmArguments&gt;

    如果您在 IDE 中运行,则将以下行添加到您的 VM 参数中。

    -javaagent:&lt;PATH TO MAVEN REPO&gt;/org/aspectj/aspectjweaver/1.8.5/aspectjweaver-1.8.5.jar

    maven repo 的路径可能类似于/User/kenny/.m2/repository/。

    $REPO 将在 POM 文件中自动解析。

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 2015-09-16
      • 2019-01-20
      • 1970-01-01
      相关资源
      最近更新 更多