開篇博客 - 優化NAYNEHC大神寫的一段Java代碼。

針對NAYNHC大神的一段Java程序代碼進行了優化,優化後的代碼更加的簡潔,並且還避免了JDBC鏈接池泄露的問題。java

優化點:mysql

1)在select操做的方法裏面,出現異常的時候,我的以爲是不須要執行session.rollback()操做的。sql

2)在全部數據庫鏈接使用完成之後,我的以爲須要執行下session.close()方法,將使用完成的鏈接從新返還到鏈接池,以免數據庫鏈接的泄露。數據庫

如下爲優化後的代碼:apache

package com.cy.mybatis.tools;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.cy.mybatis.beans.UserBean;
import com.cy.mybatis.mapper.UserMapper;
import com.cy.mybatis.service.DBTools;

public class UserService {
    
    public static void main(String[] args) {
//      insertUser();
//      deleteUser();
//      selectUserById();
        selectAllUser();
    }
    
    /**
     * 新增用戶
     */
    private static void insertUser() {
        SqlSession session = DBTools.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        UserBean user = new UserBean("lisi", "5432109876", 1100.0);
        try {
            mapper.insertUser(user);
            session.commit();
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        }
    }
    
    /**
     * 刪除用戶
     */
    private static void deleteUser(){
        SqlSession session=DBTools.getSession();
        UserMapper mapper=session.getMapper(UserMapper.class);
        try {
            mapper.deleteUser(1);
            // 增刪改須要commit()會話;
            session.commit();
            session.close(); 
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        }
    }
    
    /**
     * 根據id查詢用戶
     */
    private static void selectUserById(){
        SqlSession session=DBTools.getSession();
        UserMapper mapper=session.getMapper(UserMapper.class);
        try {
            UserBean user = mapper.selectUserById(2);
       session.commit(); session.close(); }
catch (Exception e) { e.printStackTrace(); } } /** * 查詢全部的用戶 */ private static void selectAllUser(){ SqlSession session=DBTools.getSession(); UserMapper mapper=session.getMapper(UserMapper.class); try { List<UserBean> user=mapper.selectAllUser(); System.out.println("ResultSet has " + user.size() +" rows."); for(int i=0; i<user.size();i++){ System.out.println("ID="+user.get(i).getId()+" | " + "username="+user.get(i).getUsername()+" | " + "password="+user.get(i).getPassword()+" | " + "account="+user.get(i).getAccount()); }
       session.commit(); session.close(); }
catch (Exception e) { e.printStackTrace(); } } }

 下面是增長session.close()方法後,在日誌裏面記錄到的MyBatis將使用過的JDBC鏈接返還到鏈接池的記錄。session

1
2
3
4
5
6
7
8
9
 
2019-2-15 15:38:55[DEBUG](JdbcTransaction.java:137) Opening JDBC Connection
2019-2-15 15:38:56[DEBUG](PooledDataSource.java:406) Created connection 372038137.
2019-2-15 15:38:56[DEBUG](JdbcTransaction.java:101) Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@162cd9f9]
2019-2-15 15:38:56[DEBUG](BaseJdbcLogger.java:159) == >   Preparing: select * from t_user 
2019-2-15 15:38:56[DEBUG](BaseJdbcLogger.java:159) ==
>  Parameters: 
2019-2-15 15:38:56[DEBUG](BaseJdbcLogger.java:159) 
< ==      Total: 3
2019-2-15 15:38:56[DEBUG](JdbcTransaction.java:123) Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@162cd9f9]
2019-2-15 15:38:56[DEBUG](JdbcTransaction.java:91) Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@162cd9f9]
2019-2-15 15:38:56[DEBUG](PooledDataSource.java:363) Returned connection 372038137 to pool.

 關鍵字:MyBatis,JDBC,鏈接池泄露,鏈接回收mybatis

相關文章
相關標籤/搜索