2016-05-02 6 views
3

Ich bin eine Anwendung im Frühjahr mit JPA implementieren und ich bin es im Weblogic-Server bereitstellen. Ich möchte wissen, wie man mit Transaktionen umgeht. Für die Datenbankkonfiguration habe ich persistence.xml konfiguriert, wo ich den Transaktionstyp als JTA deklariert habe. In meiner Persistenzlogik, während ich etwas aktualisiere, verwende ich diese Logik:Wie behandelt man die Transaktion im Frühjahr mit weblogic und JPA

entityManager.getTransaction().commit(); 

aber es löst eine Ausnahme aus. Wenn ich nicht festlege, werden die Daten nicht in der Datenbanktabelle aktualisiert. Auch wenn ich versuche, @Transactional auf Methodenebene zu deklarieren, funktioniert es nicht. Kann mir bitte sagen, wie man mit Transaktionen umgeht und ob ich sie richtig verwende oder nicht.

Hier sind meine Dateien.

Die DAO-Klasse:

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

      } 

     } 

Hier Datei meine Federkonfiguration ist:

<?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> 

Hier ist meine 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> 
+1

Sie sollten die 'EntityManager' injizieren, aber Sie sind eine neue erstellt , außerhalb der Sichtbarkeit von Spring, macht das '@ Transactional' unbrauchbar. Wenn Sie JTA verwenden, dürfen Sie die Transaktion nicht selbst durchführen, der Container muss dies für Sie erledigen. Ich hoffe auch, dass Sie nicht wirklich das alte verwenden (und als * veraltet * 'TransactionalProxyFactoryBean' betrachten). –

+0

Hallo Deinum, kannst du mir bitte sagen, wie man Transaktionen in sping jdbc mit weblogic sever behandelt. hier verwende ich jndi. – suri

Antwort

3

Sie erstellen Abfrage:

entityManager.createNativeQuery(query); 

Aber Sie ausführen es nicht: http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html#executeUpdate()

+0

vielen Dank für Ihre Antwort, es funktioniert gut. und sagen Sie mir eine Sache ist meine Konfiguration ist ok, ich meine persistence.xml und Spring-Konfigurationsdatei – suri

+0

Auf den kurzen Blick sieht es OK, aber sehen Sie den Kommentar zu Ihrer Frage von @ m-deinum - die 'EntityManager' Instanz sollte injiziert werden über '@ PersistenceContext' und Sie sollten die Transaktionen nicht selbst begehen – rapasoft

+0

hallo können Sie mir sagen, wie man mit der Transaktion im Falle von Frühjahr jdbc, ich meine keine jpa, und überwintern in Weblogic-Server. – suri

0

denke ich, besser in den Griff Transaktionen auf Service-Layer (es ist für Situation, wenn der Service-Nutzung mehrere dao Methoden). Und Sie sollten Ihren Entity Manager einmal erstellen, nicht für jede Dao-Methode.

Es ist ein Beispiel, wie ich Transaktionen mit jpa handle.

dao Schicht:

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

Beispiel für 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; 
} 

und Konfigurationsbeispiel:

<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> 
+0

danke für Ihre Antwort und können Sie persistence.xml Datei auch – suri

+0

bereitstellen und ich verwende Weblogic-Server zu implementieren, konfiguriert Sie Transaktion in Bezug auf jpa nicht Weblogic-Transaktion. Kannst du es erklären? – suri

+0

Es ist alles Konfiguration, Sie können @PersistenceContext Annotation anstelle von persistence.xml Datei verwenden – Ales