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
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:
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
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:
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);
}
}