简体   繁体   English

Spring Web App + JPA没有可用的事务性EntityManager

[英]Spring Web App + JPA No transactional EntityManager available

I've gone through a lot of similar question on SO, generally the answer is to just add @Transactional to the class(es) and method(s). 我在SO上经历了很多类似的问题,通常的答案是将@Transactional添加到类和方法中。 But this is not working for me, thus I assume I'm doing something else wrong? 但这对我不起作用,因此我认为我在做其他错误吗?

MyRepository: MyRepository:

@Repository
public class MyRepository extends AbstractRepository<MyEntity> implements org.springframework.data.repository.Repository<MyEntity, Long> {

    @PersistenceContext
    private EntityManager em;

    public MyRepository() {
        super(MyEnitiy.class);
    }

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    @SuppressWarnings("unchecked")
    @Transactional
    public List<MyEnitiy> getAllFor(Integer id) {        
        return getEntityManager().createQuery("SELECT k FROM MyEntity k WHERE k.otherid = :otherid ORDER BY k.something", MyEntity.class)
            .setParameter("otherid", id).getResultList();
    }
}

AbstractRepository: 摘要存储库:

@Transactional(readOnly = true)
public abstract class AbstractRepository<T> {

    ...
    @Modifying
    @Transactional
    public void edit(T entity) {
        getEntityManager().merge(entity);
    }
    ...
}

My Sping web controller (Snippet): 我的Sping Web控制器(摘要):

@Controller
public class MyController {
    @Autowired
    private MyRepository myRepository;

    @RequestMapping(value = {"/here/there"}, method = RequestMethod.GET)
    @ResponseBody
    @Transactional
    public String hereAndTherePage(@RequestParam(value = "id", required = true) Integer id,
            HttpServletRequest request, HttpSession session) {
        List<MyEntity> myEntities = myRepository.getAllFor(id);
        for(MyEntity myEntity : myEntities) {
            ...
            myEntity.setSomeValue(myEntity.getSomeValue() + 1);
            ...
            myRepository.edit(myEntity);

Update: 更新:

spring-database.xml (the use of a pool was an attempt to try and fix a Tomcat memory leak): spring-database.xml(使用池是尝试修复Tomcat内存泄漏的尝试):

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

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.example.*" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="generateDdl" value="true" />
        </bean>
    </property>
</bean>

<bean id="poolProperties" class="org.apache.tomcat.jdbc.pool.PoolProperties">
    <property name="url" value="jdbc:mysql://localhost:3306/bfwinkel?noAccessToProcedureBodies=true"/> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="validationQuery" value="SELECT 1"/>
    <property name="testOnBorrow" value="true"/>
    <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>
    <property name="maxActive" value="10"/>
    <property name="maxIdle" value="10"/>
    <property name="username" value="d[-.-]b"/>
    <property name="password" value="~!~"/>
</bean>

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
    <property name="poolProperties" ref="poolProperties"/>
</bean>

The exception is thrown when getEntityManager().merge(entity); 该异常被抛出时getEntityManager().merge(entity); is called: 叫做:

javax.persistence.TransactionRequiredException: No transactional EntityManager available

Add the below lines to your spring-database.xml 将以下几行添加到您的spring-database.xml中

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

<tx:annotation-driven transaction-manager="txManager" /> 

This error means that, they are no available persistence context available when you try to merge your entity. 此错误意味着,当您尝试合并实体时,它们没有可用的持久性上下文。

You have Three options to fix this issue by priority. 您有三个选项可按优先顺序解决此问题。

1- Have you added @EnableTransactionManagement to your configuration ? 1-您是否已将@EnableTransactionManagement添加到您的配置中?

2- If you have a persistence.xml file, you should add a unitname attribute to @PersistenceContext. 2-如果您有一个persistence.xml文件,则应将一个unitname属性添加到@PersistenceContext。

3- Use Extended Persistence context instead of Transaction-scoped Persistence context. 3-使用扩展持久性上下文,而不是事务范围的持久性上下文。

I hope my answer will help you. 希望我的回答对您有所帮助。

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 启动 + Hibernate + JPA 没有事务实体管理器可用 - Spring boot + Hibernate + JPA No transactional EntityManager available Hibernate Spring JPA javax.persistence.TransactionRequiredException:没有可用的事务性EntityManager - Hibernate Spring JPA javax.persistence.TransactionRequiredException: No transactional EntityManager available 没有事务性EntityManager可用 - 使用JPA Api,Hibernate Session出错 - No transactional EntityManager available - working with JPA Api, error with Hibernate Session @PostConstruct中没有可用的交易实体管理器 - No transactional entitymanager available in @PostConstruct 没有可用的事务性 EntityManager 异常 - No transactional EntityManager available Exception 抛出“没有可用的事务entityManager”的事务方法 - transactional method throwing “no transactional entityManager available” TransactionRequiredException:@Transactional方法中没有可用的事务EntityManager - TransactionRequiredException: No transactional EntityManager available within @Transactional method @Transactional方法后,JPA EntityManager会阻塞 - JPA EntityManager blocks after @Transactional method 持久化对象时没有可用的事务性EntityManager - No transactional EntityManager available when Persisting an object java.lang.IllegalStateException:没有可用的事务性 EntityManager - java.lang.IllegalStateException: No transactional EntityManager available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM