Declarative Transaction Management

Step 1. Configure C3P0 database connection pool 2. Configure sessionFactory factory 3. Configure transaction manager 4. Configure notification 5. Configure Aop

Configure the C3P0 database connection pool

  • The connection pool is that the database will generate the specified database link when the program is connected. When accessing the database, the database link in the connection pool will be preferentially used. When the number of links in the connection pool reaches the upper limit, the database will be obtained. The connection in the connection pool will still return to the database connection pool when the connection is closed and the time limit is reached. There are many types of connection pools, such as C3P0, Proxool, DBCP, Druid, Ali, etc.
  • In order to alleviate the multiple links of data, affect the performance of the database, and the 8-hour trap of the database, it is necessary to use the database connection pool to deal with it.
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- 初始化连接数 -->
        <property name="initialPoolSize" value="3"/>
        <!-- 最小连接数  -->
        <property name="minPoolSize" value="3"/>
        <!-- 最大连接时间 -->
        <property name="maxConnectionAge" value="28800"/>
    
    </bean>

Configure the sessionFactory factory

  • To operate the database, you must obtain the sessionFactory factory to get the session to operate the database.
  • Inject the connection pool configuration into the sessionFactory configuration.
  • <!-- 配置sessionFactory -->
       <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"  id="sessionFactory">
         <!-- 关联数据源和SessionFactory -->
          <property name="dataSource" ref="dataSource"/>
           <!-- hiberna的配置文件 -->
          <property name="hibernateProperties" >
          <props>
          <!-- 数据库方言的设置 -->
      				<prop key="hibernate.dialect">${dialect}</prop>
      				<!--设置session上下文这里不能设置thread,设置thread就需要手动管理并不能让Spring管理,要么不指定要么指定下面的类 -->
      				<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
      				<!-- 显示SQl和格式化SQl  -->
      				<prop key="hibernate.show_sql">${showSql}</prop>
          </props>    
          </property>
          <property name="packagesToScan" value="com.zhidi.entity">
          </property>
       </bean>

Configure Transaction Manager

  • The transaction manager is to centralize the transactions before and after the business logic to the transaction manager for centralized processing, which can optimize our code and avoid the opening and closing of unnecessary transactions.
  • The configuration of the transaction manager will not automatically open the transaction, and it still requires manual management and control.
<!-- 配置事务管理器 -->
     <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"/>
     </bean>

Configure notifications (below is the injection of XML)

  • Configure the transaction for the business method that needs to be executed. The value of the name attribute is the object we need to notify and also the main business logic. Configure the transaction propagation method later to determine whether this business needs to open a transaction. If the transaction is opened, the database can be added, deleted, or modified.
<!--配置通知  -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <!-- 配置增删改的方法 这里设置的方法可以自动注入 这里的name的方法可以自动搜索 可以定义多个常用的方法名 -->
    <tx:method name="save*" propagation="REQUIRED"/>
    <tx:method name="delect*" propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <!--配置查询的方法  REQUIRED表示没有逻辑事务的情况下会自己生产一个,如果有就自动加入
       SUPPORTS是如果当前有逻辑事务存在就加入到事务中,如果没有就以非事务进行处理 -->
    <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
    </tx:advice>

Configure Aops

  • The specific implementation of AOP of the configured database is injected into the corresponding place through the pointcut for reference.

    <!-- aop配置 -->
       <aop:config>
       <!--将配置好的通知嵌入到切点相关联  -->
       <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zhidi.service.impl..*.*(..))" />
       </aop:config>

Annotated pattern replaces the last two

<!-- 注解模式的注入-->
<tx:annotation-driven transaction-manager="txManager"/>

###Annotate the implementation of the configured code class. . . .

  • @Transactional does not add attribute values, it is assigned according to the default attributes, see the source code for details
  • @Transactional
      @Repository
      public class EmpDao implements EmpDaoImpl {
        
      
      	@Autowired
      	private DataSource dataSource;
          
          @Autowired
          private SessionFactory sessionFactory;
          
          
          public Session getSession() {
          	
      		return sessionFactory.getCurrentSession();
      	}
      	
      	public DataSource getDataSource() {
      		
      		return dataSource;
      	}
      	@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
      	public void get(){
      		Session session = getSession();
      		Transaction tx = session.beginTransaction();
      		   Emp emp = (Emp) session.get(Emp.class, 2);
      		   System.out.println(emp);
      		tx.commit();
      	}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326422651&siteId=291194637