简体   繁体   English

如何使用weblogic和JPA处理Spring中的事务

[英]How to handle transaction in Spring with weblogic and JPA

I am implementing an application in Spring with JPA and I am deploying it in weblogic server. 我正在使用JPA在Spring中实现一个应用程序,我将它部署在weblogic服务器中。 I want to know how to handle transactions. 我想知道如何处理交易。 For database configuration I configured persistence.xml where I declared the transaction type as JTA. 对于数据库配置,我配置了persistence.xml,其中我将事务类型声明为JTA。 In my persistence logic, while updating something, I am using this logic: 在我的持久性逻辑中,在更新某些东西时,我正在使用这个逻辑:

entityManager.getTransaction().commit();

but it throws an exception. 但它引发了一个例外。 If I don't commit the data is not updating in the database table. 如果我不提交数据不在数据库表中更新。 Even if I try with declaring @Transactional at method level it is not working. 即使我尝试在方法级别声明@Transactional它也无法正常工作。 Can any body please tell me how to handle transactions and if I am using them correctly or not. 任何人都可以告诉我如何处理交易,如果我正确使用它们。

Here are my files. 这是我的文件。

The DAO class: DAO课程:

  @Override
        @Transactional
        public void updateBpm(User user) {
            EntityManager entityManager=null;
            try{
             entityManager=entityManagerFactory.createEntityManager();
            String  query="update com_tt_bpm_batch set status = 'FAILED' where  seqNo="+user.getSeqNo();
            entityManager.createNativeQuery(query);
            System.out.println("table updated Successfully..");

                }
            catch(Exception e){
                logger.error(e.getStackTrace());

            }

        }

Here is my spring configuration file: 这是我的spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.2.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    ">
    <tx:annotation-driven />
    <context:annotation-config/>
    <mvc:annotation-driven/>
     <context:component-scan base-package="com.tcs" /> 


        <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
             <property name="persistenceXmlLocation" value="/WEB-INF/META-INF/persistence.xml" /> 
             <property name="persistenceUnitName" value="Mypersist" /> 
            <!--  <property name="dataSource" ref="dataSource" /> -->
             <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
             <property name="jpaDialect" ref="jpaDialect" />
             <property name="jpaProperties">
            <props>
                <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup
                </prop>
            </props>
        </property> 
        </bean>

      <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    abstract="true">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributes">
      <props>
        <prop key="updateBpm">PROPAGATION_REQUIRED</prop>
        <prop key="getforBpm">PROPAGATION_REQUIRED</prop>
      </props>
    </property>
  </bean>
        <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
          <!--  <property name="database" value="oracle" /> -->
           <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
       </bean>

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />



 <bean id="transactionManager"
    class="org.springframework.transaction.jta.WebLogicJtaTransactionManager">
    <property name="transactionManagerName"
      value="javax.transaction.TransactionManager"/>
  </bean>


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

Here is my persistence.xml 这是我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="Mypersist" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
         <jta-data-source>MCDataSource</jta-data-source>

    </persistence-unit>


</persistence>

You are creating query: 您正在创建查询:

entityManager.createNativeQuery(query);

But you are not executing it: http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html#executeUpdate() 但是你没有执行它: http//docs.oracle.com/javaee/6/api/javax/persistence/Query.html#executeUpdate()

I think better handle transactions on service layer (it's for situation when your service use several dao methods). 我认为更好地处理服务层上的事务(当您的服务使用多个dao方法时)。 And you should create your entity manager once, not for every dao method. 你应该创建一次实体管理器,而不是每个dao方法。

It's example how I handle transactions with jpa. 这是我如何使用jpa处理事务的示例。

dao layer: dao层:

@PersistenceContext
private EntityManager entityManager;

@Override
public String create(Brand brand) throws DaoException {
    try {
        entityManager.persist(brand);
    } catch (HibernateException e) {
        throw new DaoException(e);
    }
    return brand.getId();
}

example of service layer: 服务层的示例:

@Service
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private IBrandDao brandDao;

    @Override
    public TransportObject create(Brand brand) {
        TransportObject transportObject = null;
        try {
           // some code here
        } catch (DaoException e) {

        }
        return transportObject;
}

and configuration example: 和配置示例:

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="false"/>
        <property name="generateDdl" value="false"/>
        <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
    </bean>

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <value>package path</value>
        </property>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />
    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf" />
    </bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM