【问题标题】:Spring - PersistenceContext - No transactional EntityManager availableSpring - PersistenceContext - 没有可用的事务性 EntityManager
【发布时间】:2015-01-31 21:15:09
【问题描述】:

我在 JPA 中使用 spring,我所有的 getter 都在工作。我可以从数据库中获取对象,但是当我尝试保存某些内容时,我收到错误“没有可用的事务性 EntityManager”,并且我有事务的注释。

这是我的 DAO:

    @Repository("userDao")
    @Transactional
    //public class UserDaoImpl extends GenericDaoImpl<User, Long> implements IUserDao {
    public class UserDaoImpl implements IUserDao {  

        final protected Logger logger = LoggerFactory.getLogger(this.getClass());

        public UserDaoImpl() {
        }

        @PersistenceContext(unitName = "ovdigitalPU")
        protected EntityManager entityManager;

    @Override
        @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
        public void save(User user) throws DuplicatedUserException {
            logger.info("Creating User with username: " + user.getUsername());
            User foundUser = null;
            logger.info("Checking if user already existis...");
            /*foundUser = this.findByUsername(user.getUsername());

            if(foundUser != null) {
                throw new DuplicatedUserException();
            }*/

            entityManager.persist(user);
            logger.info("User " + user.getUsername() + " was been saved with success!");

        }

这是我的 root-context.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->
    <context:annotation-config />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="ovdigitalDS"
        class="org.apache.commons.dbcp.BasicDataSource"
        p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost/ovdigital"
        p:username="ovdigital" p:password="12345">
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="ovdigitalDS" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
    </bean>

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

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:spring-configured />
    <context:annotation-config />

</beans>

还有我的 persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
      <persistence-unit name="ovdigitalPU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
          <property name="hibernate.transaction.auto_close_session" value="false"/>
          <property name="hibernate.show_sql" value="true"/>
          <property name="hibernate.format_sql" value="false"/>
          <property name="hibernate.generate_statistics" value="false"/>
          <property name="hibernate.connection.autocommit" value="true"/>
          <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        </properties>
      </persistence-unit>
    </persistence>

在调试器模式下,当服务器尝试执行此行时 - entityManager.persist(user);我得到这个exception javax.persistence.TransactionRequiredException: No transactional EntityManager available

我能做些什么来解决我的问题?

【问题讨论】:

  • 您使用的是哪个事务注释?检查你的进口它应该是春天
  • 我正在使用来自 spring 的事务(import org.springframework.transaction.annotation.Transactional)
  • 但例外是“javax.persistence.TransactionRequiredException.: No transactional EntityManager available”。为什么服务器不使用 spring 的事务?
  • 问题已解决。我创建了一些带有注释的bean,组件扫描在spring上下文中,数据源配置在另一个上下文中。我把它分开了,现在它可以工作了:)

标签: database spring hibernate jpa


【解决方案1】:

这太棒了,但对我来说,方法的签名让一切变得不同:

@Transactional
void save(Entity entity) //throw No transactional EntityManager available

@Transactional
public void save(Entity entity) //ok

【讨论】:

  • 因为您在第一种情况下它是私有方法,而 @Transactional 对非公共方法没有影响
【解决方案2】:

我遇到了这个问题,其他东西为我解决了这个问题。

我正在使用全 Java 配置 (no-xml),并且忘记了我的配置类上的 @EnableTransactionManagement 注释。就是这样。

【讨论】:

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