20 Mayıs 2018 Pazar

SpringData HibernateTransactionManager Sınıfı

Giriş
Şeklen şöyle


Eğer hiyerarşi olarak bakarsak şeklen şöyle. Burada tüm TransactionManager sınıflarının AbstractTransactionManager'dan kalıttığı görülebilir.



Bu sınıfın çalışması için
1. DataSource - örneğin org.springframework.jdbc.datasource.DriverManagerDataSource
2. org.springframework.orm.hibernate5.LocalSessionFactoryBean
sınıfları lazım

Örnek - hibernate5
Örnek
Şöyle yaparız.
<bean id="sessionFactory"
  class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="mappingResources">
    <list>
      <value>org/springframework/samples/petclinic/hibernate/petclinic.hbm.xml</value>
    </list>
  </property>
  <property name="hibernateProperties">
    <value>
      hibernate.dialect=${hibernate.dialect}
    </value>
  </property>
</bean>

<bean id="txManager"
  class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
Örnek
DataSource+ SessionFactory + TransactionManager tanımlanır. Şöyle yaparız.
<!-- setting up my data Source -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  <property name="jdbcURL" value="jdbc:myql://localhost:3306/customer"/>
  <property name="user" value="springstudent"/>
  <property name="password" value="springstudent"/>

  <property name="minPoolSize" value="5"/>
  <property name="maxPoolSize" value="20"/>
  <property name="maxIdleTime" value="30000"/>

</bean>

<!-- Setting up my sessionFactory -->
<bean id="sessionFactory"
  class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  <property name="dataSource" ref="myDataSource"/>
  <property name="packagesToScan" value="com.luv2code.springtutorial.entity"/>
  <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.mysqlDialect</prop>
      <prop key="hibernate.show_sql">true</prop>
    </props>
  </property>
</bean>


<bean id="myTransactionManager"
  class="org.hibernate.orm.hibernate5.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="myTransactionManager"/>
Örnek - hibernate4
DataSource+ SessionFactory + TransactionManager tanımlanır. Şöyle yaparız.
<!-- Database Information -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url"
              value="jdbc:mysql://localhost:3306/DayDatabase"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate4AnnotatedSessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan">
        <list>
            <value>net.yan.model</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>


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

<bean id="transactionManager" 
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
</bean>
Örnek - hibernate4
Şöyle yaparız.
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost:3306/test"/>
  <property name="username" value="root"/>
  <property name="password" value=""/>
</bean>

<bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan">
    <list>
      <value>dk.accunu.model</value>
     </list>
    </property>
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

<bean id="transactionManager"
  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>
Örnek - hibernate3
Şöyle yaparız.
<bean id="transactionManager"   
class="org.springframework.orm.hibernate3.HibernateTransactionManager">    
<property name="sessionFactory" ref="sessionFactory"/>    
</bean>    
<tx:annotation-driven/>
constructor
Şöyle yaparız.
@Bean
public HibernateTransactionManager transactionManager(SessionFactory sf) {
  return new HibernateTransactionManager(sf);
}

Hiç yorum yok:

Yorum Gönder