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
What error do you get when you try to persist?
The LoadTimeWeaver should work, but it is not required. You could use static weaving instead.
Thanks, James.
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 into $CATALINA_HOME/lib, where $CATALINA_HOME represents the root of the Tomcat installation)
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>
I didn't specify on path and docBase attribute as I put in $CATALINA_HOME/conf/context.xml
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>
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:
Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
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.
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.
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>
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()>>>");
}
}
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);
}
}
As an additional way to @jisun's reply.I did it by annotation configuration and removed more properties such as username,password,driver etc.Because all of these parameters can be define in persistence.xml:
package com.company.config;
import javax.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.core.env.Environment;
import org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver;
import org.springframework.orm.jpa.JpaTransactionManager;
@Configuration
@ComponentScan(basePackages = "com.company")
@EnableTransactionManagement
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
@Resource
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setPersistenceXmlLocation("classpath:persistence.xml");
emf.setLoadTimeWeaver(new ReflectiveLoadTimeWeaver());
return emf;
}
@Bean
public JpaTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory().getObject());
}
}