如何配置EclipseLink 2.0和Spring 3.0.5以及Tomcat 6? [英] How to configure EclipseLink 2.0 and Spring 3.0.5 and Tomcat 6?

查看:81
本文介绍了如何配置EclipseLink 2.0和Spring 3.0.5以及Tomcat 6?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序使用Tomcat 6.0.18和Spring 3.0.5以及eclipselink 2.0.1和javax.persistence 2.0.0,SQL Server数据库。我无法弄清楚配置,也无法找到具有此类配置的工作示例。我尝试将loadTimeWeaver的属性添加到entityManagerFacotory中,但它在Spring 3中打破了AutoWired批注,如下面的applicationContext.xml中所示:

My web application is using Tomcat 6.0.18 and Spring 3.0.5 and eclipselink 2.0.1 and javax.persistence 2.0.0, SQL Server Database. I could not figure out the configuration and also not able to find out a working example with such configurations. I tried to add property of loadTimeWeaver into entityManagerFacotory but it break AutoWired annotation in Spring 3, like below in applicationContext.xml:

<context:load-time-weaver/>




















in appname-servlet.xml:

但是当我禁用LoadTimeWeaver时,我的应用程序可以从JPA代码创建数据库,但无法将数据保存到数据库中。

But when I disable LoadTimeWeaver, my application can create database from JPA code but could not persist data into database.

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
        <property name="dataSource" ref="dataSource" />         
        <property name="persistenceUnitName" value="restfulPU" />
        <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="true"/>
            <property name="databasePlatform" value="org.eclipse.persistence.platform.database.SQLServerPlatform"/>
        </bean>
        </property>
        <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
        </property>         
        <property name="jpaPropertyMap">
        <props>
            <prop key="eclipselink.weaving">false</prop>
        </props>
        </property>         
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean> 
<tx:annotation-driven transaction-manager="transactionManager"/>
  Persistence.xml
<persistence-unit name="restfulPU" transaction-type="RESOURCE_LOCAL">
    <class>com.generalform.eclipselink.model.Todo</class>
    <properties>
        <!-- EclipseLink should create the database schema automatically -->
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
        <property name="eclipselink.ddl-generation.output-mode"
           value="database" />
    </properties>

如果您能指导我将有关EclipseLink集成到Spring 3的指南或教程,我将不胜感激加上Tomcat。

I will be thankful if you can point me to a guide or tutorial about integration of EclipseLink into Spring 3 plus Tomcat.

推荐答案

谢谢你,詹姆斯。

关注Springsource指导在 http://static.springsource.org/spring/docs/3.0 .0.M4 / reference / html / ch13s05.html ,Tomcat 6适用于编织。指南中提到的步骤并在此处复制:

Following Springsource guidance at http://static.springsource.org/spring/docs/3.0.0.M4/reference/html/ch13s05.html, Tomcat 6 works on the weaving. The steps as mentioned in the guidance and copied here:

Step1.Copy spring-tomcat-weaver.jar到$ CATALINA_HOME / lib,其中$ CATALINA_HOME表示根的Tomcat安装)

Step1.Copy spring-tomcat-weaver.jar into $CATALINA_HOME/lib, where $CATALINA_HOME represents the root of the Tomcat installation)

Step2。通过修改context.xml告诉Tomcat使用自定义类加载器:

Step2. Tell Tomcat to use custom classloader by modifying context.xml:

<Context path="/myWebApp" docBase="/my/webApp/location">
    <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
        useSystemClassLoaderAsParent="false"/>
</Context>

我没有在路径和docBase属性上指定,因为我输入$ CATALINA_HOME / conf / context。 xml

I didn't specify on path and docBase attribute as I put in $CATALINA_HOME/conf/context.xml

Step3。将loadTimeWeaver属性设置为LocalContainerEntityManagerFactoryBean

Step3. Turn on loadTimeWeaver property to LocalContainerEntityManagerFactoryBean

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="loadTimeWeaver">
    <bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
  </property>
</bean> 

然后,我再次启动Tomcat 6,启动过程变得干净。但是,数据仍然无法持久保存到数据库中。错误如下所示:

then, I started Tomcat 6 again and start-up process becomes clean. However, data still could not be persisted into database. The error is like below:

不允许在共享EntityManager上创建事务 - 使用Spring事务或EJB CMT

我的同事通过指出使用@Transactional处理Spring事务而不使用实体管理器上的事务来避免上述问题,从而为我节省了时间。然后我注释掉em.getTransaction()。begin()和em.getTransaction()。commit(),只留下em.persist(todo)。 todo是这里的实体。它立即起作用。在这里,开发人员应该意识到JPA事务和Spring事务之间的区别。事实上,当EclipseLink / JPA使用Spring Transaction Management时,这是一个困惑的部分。

My co-worker saved me at this point by pointing out that Spring transaction handling with @Transactional and not use transactions on the entity manager to avoid the issue above. then I commented out em.getTransaction().begin() and em.getTransaction().commit(), leaving em.persist(todo) only. todo is the entity here. It works immediately. Here, developer should be aware that the difference between JPA transaction and Spring transaction. In fact, it's a confused part when EclipseLink/JPA working with Spring Transaction Management.

我也尝试过Tomcat 7,因为我认为它可能与Tomcat有关。实际上,这个问题与Tomcat版本无关。

I also tried Tomcat 7 as I had thought it might be related to Tomcat. In fact, this issue is nothing to do with Tomcat version.

启用LoadTimeWeaver后,它可以处理数据持久性。以下是applicationname-servlet.xml中transactionManager配置部分的工作版本:

With LoadTimeWeaver enabled, It works on data persistence. Here is the working version on the transactionManager configuration part in applicationname-servlet.xml:

   <context:property-placeholder location="classpath:generalform.properties"/>
   <context:component-scan base-package="com.generalform" />

   <tx:annotation-driven transaction-manager="transactionManager"/> 
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${mysql.database.driver}" />
    <property name="url" value="${mysql.database.url}" />
    <property name="username" value="${mysql.database.user}" />
    <property name="password" value="${mysql.database.password}" />
</bean>

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
        <property name="dataSource" ref="dataSource" />         
        <property name="persistenceUnitName" value="restfulPU" />
        <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="true"/>
            <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
        </bean>
        </property>
        <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
        </property>
        <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" />
        </property>         

</bean> 

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

下面是我的Dao类,正式方式,try / catch应该包含在里面的代码里面方法:

Below is my Dao class, in formal way, try/catch should be wrapped around the code inside the method:

@Repository("todoDao")
public class TodoDao {  

    @PersistenceContext
    private EntityManager em;


    public void saveTodo(Todo todo) {

        System.out.println("TodoDao:saveTodo into DB >>>");
        //em.getTransaction().begin();
        em.persist(todo);
        //em.getTransaction().commit();
        em.close();
        System.out.println("TodoDao: complete saveTodo into DB close()>>>");
    }   

}   

TodoService类声明Spring Transaction with @Transactional注释,@ Autowired也在启用LoadTimeWeaver后工作:

The TodoService class declares Spring Transaction with @Transactional annotation, @Autowired is also working after enabling LoadTimeWeaver:

@Service("todoService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class TodoService {
    @Autowired
    private TodoDao todoDao;

    public TodoService() {
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void saveTodo(Todo todo) {
        System.out.println("TodoService -> saveTodo is called!");
        todoDao.saveTodo(todo);
    }
}   

这篇关于如何配置EclipseLink 2.0和Spring 3.0.5以及Tomcat 6?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆