如何配置SharedEntityManagerCreator每次都不创建实体管理器..

时间:2012-07-20 15:17:12

标签: jta

我在删除实体时遇到了一个奇怪的问题,我在想 这是因为错误的弹簧配置/错误,我无法做到 确切地指出它是什么。我能够做读,但每当我尝试 删除我收到错误..

以下是我的设置..我有一个基于弹簧的交易管理委托 到jboss的jta。以下是我的弹簧配置

<context:annotation-config />
<context:component-scan base-package="com.blah.blah">
    <context:exclude-filter type="regex"
        expression="com.blah.blah.batch.*" />
</context:component-scan>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="pmabDataSource" />
    <property name="persistenceUnitName" value="MABPersistenceEl" />
    <property name="jpaDialect" ref="eclipseLinkDialect" />
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence-eclipse.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>

    <property name="jpaPropertyMap">
        <map>
            <entry key="eclipselink.target-server" value="JBOSS" />
            <entry key="eclipselink.target-database" value="Oracle" />
            <entry key="eclipselink.persistence-context.flush-mode" value="AUTO" />
            <entry key="eclipselink.jdbc.native-sql" value="false" />
            <entry key="eclipselink.weaving" value="false" />
            <entry key="eclipselink.logging.level" value="FINEST" />
            <entry key="eclipselink.logging.parameters" value="true" />
            <entry key="eclipselink.logging.exceptions" value="true" />
            <entry key="eclipselink.orm.throw.exceptions" value="true" />
        </map>
    </property>
</bean>

<bean id="eclipseLinkDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />

<tx:jta-transaction-manager />

持久性文件如下

<persistence version="1.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_1_0.xsd">

    <persistence-unit name="MABPersistenceEl"
        transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>pmabDataSource</jta-data-source>
        <!-- ,,,, other things ommited for brevity/> -->
    </persistence-unit>
</persistence>

我的basedao会像这样注入EntityManager

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

在我的派生道中的某处,我有以下

UserRole newRole=entityManager.find(UserRole.class,urole.getId()) ;
entityManager.remove(newRole);

我得到以下内容 09:53:00,386调试 为共享的EntityManager调用创建新的EntityManager 09:53:00,413 DEBUG [EntityManagerFactoryUtils]:328关闭JPA EntityManager的 09:53:04119 DEBUGSharedEntityManagerCreator $ SharedEntityManagerInvocationHandler:231 为共享的EntityManager调用创建新的EntityManager 09:53:04,146 DEBUG [EntityManagerFactoryUtils]:328关闭JPA EntityManager的 09:53:05,760调试 为共享的EntityManager调用创建新的EntityManager 09:53:05,781 DEBUG [EntityManagerFactoryUtils]:328关闭JPA EntityManager的

这是弹簧EntityManager创建者每个都创建一个entityManager 访问它的时间,而不是返回事务的时间 。这导致EL库变得狂暴。 java.lang.IllegalArgumentException:必须管理实体才能调用 删除:UserRole @ 419d,尝试合并分离并尝试删除 再次。 在 org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.performRemove(UnitOfWorkImpl.java:3559) 在 org.eclipse.persistence.internal.jpa.EntityManagerImpl.remove(EntityManagerImpl.java:518)

如何配置spring实体管理器创建者以回馈我 事务的实体管理器而不是每个创建一个 时间 ?

1 个答案:

答案 0 :(得分:0)

我做了以下更改,它现在有效.. 代码:

<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
<tx:advice id="pmabTxAdvice" transaction-manager="transactionManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
        <!-- all methods starting with 'get' are read-only -->
        <!-- other methods use the default transaction settings (see below) -->
      <tx:method name="*" propagation="REQUIRED" read-only="false"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="serviceOperation"
        expression="execution(* com.foo.foobar.abc.*.*.*(..))" />
    <aop:advisor advice-ref="pmabTxAdvice" pointcut-ref="serviceOperation" />
</aop:config>

希望这有助于某人