24 Ekim 2018 Çarşamba

SpringData JpaTransactionManager Sınıfı

Giriş
Şu satırı dahil ederiz.
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.orm.jpa.JpaTransactionManager;
PlatformTransactionManager arayüzünden kalıtır. Şu satırı dahil ederiz.
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
Normalde bu sınıfı tanımlamaya gerek yok. Eğer birden fazla veri tabanına bağlanmak gerekiyorsa tanımlamak gerekir. 

constructor
Şöyle yaparız.
JpaTransactionManager txManager = new JpaTransactionManager();
setDataSource metodu
Bu metod niye var bilmiyorum çünkü LocalContainerEntityManagerFactoryBean sınıfında da aynı metod var ve LocalContainerEntityManagerFactoryBean nesnesi setEntityManagerFactory () metodu ile bu sınıfa geçiliyor.
Örnek
Şöyle yaparız.
DataSource ds = ...;
txManager.setDataSource(this.dataSource);
setEntityManagerFactory metodu
Örnek
Şöyle yaparız.
EntityManagerFactory emf = ...;
txManager.setEntityManagerFactory(this.entityManagerFactory);
Örnek
Şöyle yaparız.
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
 LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
 ...
 return entityManager;
}

@Bean
public JpaTransactionManager transactionManager(@Autowired EntityManagerFactory em) {
  JpaTransactionManager transactionManager = new JpaTransactionManager();
  transactionManager.setEntityManagerFactory(em);
  return transactionManager;
}
Örnek
Şöyle yaparız.
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf,
  DataSource dataSource){
  JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
  jpaTransactionManager.setDataSource(dataSource);
  jpaTransactionManager.setEntityManagerFactory(emf);
  return jpaTransactionManager;
}
setGlobalRollbackOnParticipationFailure metodu
Şöyle yaparız.
@Bean
@Primary
public JpaTransactionManager transactionManager() {
  JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
  jpaTransactionManager.setEntityManagerFactory(entityManagerFactory());
  jpaTransactionManager.setGlobalRollbackOnParticipationFailure(false);
  return jpaTransactionManager;
}
Diğer
XML Olarak Tanımlama
Örnek
Şöyle yaparız.
<!-- JPA Entity Manager Factory -->
<bean id="entityManagerFactory" 
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
  p:dataSource-ref="feDataSource"/>

<!-- Transaction Config -->
<bean id="transactionManager"
  class="org.springframework.orm.jpa.JpaTransactionManager"
  p:entityManagerFactory-ref="entityManagerFactory"/>

<tx:annotation-driven mode="aspectj"                        
transaction-manager="transactionManager"/>
Örnek - JpaTransactionManager
Elimizde LocalContainerEntityManagerFactoryBean bean olsun.
<bean id="myEntityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
  p:persistenceXmlLocation="classpath*:META-INF/tune-persistence.xml"
  p:persistenceUnitName="tunePersistenceUnit" 
  p:loadTimeWeaver-ref="loadTimeWeaver"
  p:jpaVendorAdapter-ref="jpaVendorAdapter" p:jpaDialect-ref="jpaDialect"
  p:dataSource-ref="tuneDbDataSource">
  <property name="jpaProperties">
    <props>
      <prop key="hibernate.transaction.manager_lookup_class">...
      </prop>
      <prop key="net.sf.ehcache.configurationResourceName">...</prop>
      <prop key="hibernate.transaction.flush_before_completion">false</prop>
      <prop key="hibernate.default_schema">${tune-db.schema}</prop>
      <prop key="org.hibernate.envers.default_schema">${tune-db.schema}</prop>
      <prop key="javax.persistence.validation.mode">...</prop>
      <prop key="hibernate.connection.isolation">3</prop>
      <prop key="hibernate.connection.release_mode">auto</prop>
      <prop key="hibernate.show_sql">${tune-db.hibernate.show-sql}</prop>
      <prop key="hibernate.format_sql">${tune-db.hibernate.format-sql}</prop>
    </props>
  </property>     
</bean>

Şöyle yaparız.
<bean id="transactionManager"
  class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="myEntityManagerFactory" />
</bean>

Hiç yorum yok:

Yorum Gönder