春天jpa交易业务到道

时间:2011-08-19 21:59:32

标签: spring jpa transactions

业务层:

public class ServiceImpl implements Service{

  @Transactional(readOnly = false)
   public void createOrUpdateAppPing(String s) {

      servicePingDao.createOrUpdateAppPing(s);
   }
}

Dao图层

public   void createOrUpdateAppPing(String sc) {
      EntityManager  entityManager = entityManagerFactory.createEntityManager();    
          pingScService(sc,entityManager);
          if(ifHasPingScConfig(sc,entityManager)) {
             updateScConfig(sc,entityManager);
          } else {
             createScConfig(sc,entityManager);      }
         entityManager.close();    
   }

这里: ** pingScService有选择查询 ** updateScConfig - 更新查询  ** createScConfig-insert

 config service-datasorce.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
    xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
    xmlns:ctx="http://www.springframework.org/schema/context"   
    xsi:schemaLocation="
    http://www.springframework.org/schema/osgi
          http://www.springframework.org/schema/osgi/spring-osgi.xsd
    http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/osgi-compendium 
      http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd     
       ">

       <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="smx4" />
            <property name="jpaVendorAdapter" ref="jpaAdapter" />
            <property name="dataSource" ref="dataSource" />
        </bean>

        <bean id="jpaAdapter"
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="com.luthresearch.savvyconnect.model.PostgreSQLDialectUuid" />
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
    </bean> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">       
            <property name="driverClassName" value="${database.driver.class}" />        
            <property name="url"             value="${database.savvyconnect.url}" />
            <property name="username"        value="${database.savvyconnect.username}" />
            <property name="password"        value="${database.savvyconnect.password}" />

    </bean>



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

  </bean>


  IN service.xml


  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
       xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/osgi
                        http://www.springframework.org/schema/osgi/spring-osgi.xsd
                               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


    <bean id="service"
        class="com.services.impl.SServicempl" autowire="byName">

    </bean>
            <tx:annotation-driven transaction-manager="transactionManager"/>
  </beans>

现在我想在服务层添加事务支持......我怎么能这样做。

我尝试添加@Transaction注释,但我正在

  

javax.persistence.TransactionRequiredException:执行更新/删除查询

0 个答案:

没有答案