Announcement Announcement Module
Collapse
No announcement yet.
Spring and JPA - how to configure a transaction manager? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring and JPA - how to configure a transaction manager?

    Could someone tell me how I enable a transaction when using Spring and JPA.

    I followed the IBM tutorial:
    http://www.ibm.com/developerworks/ed...spring2-i.html

    and I can run JUnit tests (extends AbstractJpaTests) to store new java objects, but when I try to do

    Code:
    public static void main(String[] args) {
       String[] contextPaths = new String[] {"com/test/jpa-spring2-service.xml"};
       ApplicationContext context = new ClassPathXmlApplicationContext(contextPaths);
       MessageDAO service = (MessageDAO)context.getBean("messageService");
       Message message = new Message("test");
       service.save(message);
    }
    No objects are persisted in the database. The findAll does work.

    I use the following service code:

    Code:
    public class MessageDAO extends JpaDaoSupport implements MessageService {
        public Message save(Message message) {
            getJpaTemplate().persist(message);
            return message;
        }
    }
    I can get it to work by changing the service to:

    Code:
    public class MessageDAO extends JpaDaoSupport implements MessageService {
        public Message save(Message message) {
            getJpaTemplate().setEntityManager(getJpaTemplate().getEntityManagerFactory().createEntityManager());
            getJpaTemplate().getEntityManager().getTransaction().begin();
            getJpaTemplate().persist(message);
            getJpaTemplate().getEntityManager().getTransaction().commit();
            return message;
    }
    }
    But this is of course not the way to go!

    My com/test/jpa-spring2-service.xml is:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           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.xsd">
    
       <bean id="messageService" class="com.test.MessageDAO">
          <property name="entityManagerFactory" ref="entityManagerFactory"/>
       </bean>
       
       <bean id="logRecordService" class="com.test.LogRecordDAO">
          <property name="entityManagerFactory" ref="entityManagerFactory"/>
       </bean>
       
       <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
          <property name="dataSource" ref="dataSource"/>
          <property name="jpaVendorAdapter">
             <bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
                <property name="showSql" value="true"/>
                <property name="generateDdl" value="true"/>
                <property name="databasePlatform" value="oracle.toplink.essentials.platform.database.HSQLPlatform"/>
             </bean>
          </property>
          <property name="loadTimeWeaver">
             <!-- <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/> -->
             <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> 
          </property>
       </bean>
    
       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
          <property name="url" value="jdbc:hsqldb:hsql://localhost/xdb"/>
          <property name="username" value="sa" />
          <property name="password" value="" />
       </bean>
    
       <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
          <property name="entityManagerFactory" ref="entityManagerFactory"/>
          <property name="dataSource" ref="dataSource"/>
       </bean>
    </beans>
    See also my question at:
    http://forums.java.net/jive/thread.j...ssageID=228366

    Could someone help me to configure a transaction? Or tell me what I do wrong? Thank you!

  • #2
    Hi,

    I used the JPA Transaction Manager and the @Transactional annotation, its pretty easy to use....have enclosed my configuration file


    <tx:annotation-driven/>

    <!-- EntityManagerFactory -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
    <property name="jpaProperties">
    <value>
    hibernate.cache.provider_class=org.hibernate.cache .EhCacheProvider
    hibernate.generate_statistics=true
    hibernate.cache.use_second_level_cache=true
    hibernate.cache.use_query_cache=true
    </value>
    </property>
    </bean>

    <bean id="persistenceUnitManager" class="com.iflexsolutions.wm.infra.persistence.WMC ustomPersistenceUnitManager">
    <property name="persistenceXmlLocations">
    <list>
    <value>classpath*:META-INF/persistence.xml</value>
    </list>
    </property>
    <property name="defaultDataSource" ref="dataSource"/>
    </bean>


    <!-- For recozgnizing @PersistenceUnit annotation -->
    <bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor"/>

    <!-- Transaction manager -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    Comment

    Working...
    X