ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-如何使用weblogic和JPA在Spring中处理事务

2019-11-18 18:19:30  阅读:240  来源: 互联网

标签:jpa hibernate weblogic spring java


我正在使用JPA在Spring中实现一个应用程序,并将其部署在weblogic服务器中.我想知道如何处理交易.对于数据库配置,我配置了persistence.xml,在其中将事务类型声明为JTA.在我的持久性逻辑中,在更新某些内容时,我正在使用以下逻辑:

entityManager.getTransaction().commit();

但这会引发异常.如果我不提交,则数据库表中的数据不会更新.
即使我尝试在方法级别声明@Transactional也不起作用.
任何机构都可以告诉我如何处理交易,以及我是否正确使用了交易.

这是我的文件.

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());

            }

        }

这是我的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>

这是我的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>

解决方法:

您正在创建查询:

entityManager.createNativeQuery(query);

但是您没有执行它:http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html#executeUpdate()

标签:jpa,hibernate,weblogic,spring,java
来源: https://codeday.me/bug/20191118/2030303.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有