JDBC utils tool class - manageable transactions

/**

This blog is created by Leprosy equisetifolia It is not easy to create. Please indicate the source of Reprint:

http://blog.csdn.net/ma_shou_feng

*/

1. Tool design details

1.1 about abnormality:

The tool class is designed to provide convenience for development. If there is an exception within the tool class, it is better to throw the exception and force the caller to handle it. Otherwise, it is difficult to find the problem.

——Tell the caller that there may be a problem with the method to be used.

Although it is more flexible to throw runtime exception, the caller can handle it or not, which increases the difficulty of program maintenance.

1.2 specific practice details:

General tool classes do not need to be inherited, so add the final attribute:

public  final  class XxxUtil{...}
Similarly, there are few requirements for tool classes to be instantiated, so the construction method is privatized:
private  XxxUtil(){};

2. Implementation of JDBC utils

/**  
 * JDBC Tools  
 * @author healchow  
 */    
public final class JDBCUtils {    
    // Create a private static thread local class object to ensure that the thread is bound to the Connection object    
    private static ThreadLocal<Connection> local = new ThreadLocal<Connection>();    
    // Create private public static data source objects    
    private static DataSource dataSource = new ComboPooledDataSource();    
    
    /**  
    * Create tool class instance not allowed  
    */    
    private JDBCUtils() {}    
    
    /**  
    * Get data source object  
    * @return DataSource object  
    */    
    public static DataSource getDataSource() {    
        return dataSource;    
    }    
    
    /**  
    * Get connection object  
    * @return Connection object  
    */    
    public static Connection getConnection() {    
        try {    
            // Get connection object from thread local class object    
            Connection conn = local.get();    
            // If not, create a new connection object    
            if (conn == null) {    
                // Create a new connection object and add it to the thread local object    
                conn = dataSource.getConnection();    
                local.set(conn);    
            }    
            return conn;    
        } catch (SQLException e) {    
            //Convert compile time exceptions to run time exceptions    
            throw new RuntimeException(e);    
        }    
    }    
    
    /**  
    * Open transaction  
    */    
    public static void beginTransaction() {    
        try {    
            //Call this type of method to get the connection object    
            Connection conn = getConnection();    
            if(conn != null) {    
                conn.setAutoCommit(false);    
            }    
        } catch (SQLException e) {    
            e.printStackTrace();    
            throw new RuntimeException();    
        }    
    }    
    
    /**  
    * Commit transaction  
    */    
    public static void commitAndRelease() {    
        try {    
            //Commit the transaction and close the connection object    
            Connection conn = getConnection();    
            if(conn != null) {    
                conn.commit();    
                conn.close();    
                //Remove the connection object from local    
                local.remove();    
            }    
        } catch (SQLException e) {    
            e.printStackTrace();    
            throw new RuntimeException(e);    
        }    
    }    
    
    /**  
    * Rollback transaction  
    */    
    public static void rollbackAndRelease() {    
        try {    
            // Roll back the transaction and close the connection object    
            Connection conn = getConnection();    
            //DbUtils.rollbackAndCloseQuietly(conn);    
            if(conn != null) {    
                conn.rollback();    
                conn.close();    
                //Remove the connection object from local    
                local.remove();    
            }    
        } catch (SQLException e) {    
            e.printStackTrace();    
            throw new RuntimeException(e);    
        }    
    }    
    
    /**  
    * Close all kinds of resources  
    * @param rs  
    * @param stmt  
    * @param conn  
    */    
    public static void close(ResultSet rs, Statement stmt, Connection conn) {    
        try {    
            if (rs != null)    
                rs.close();    
        } catch (SQLException e) {    
            e.printStackTrace();    
        }    
        try {    
            if (stmt != null)    
                stmt.close();    
        } catch (SQLException e) {    
            e.printStackTrace();    
        }    
        try {    
            if (conn != null)    
                conn.close();    
        } catch (SQLException e) {    
            e.printStackTrace();    
        }    
    }
    
    // Turn off resources (Statement and connection only)    
    public static void close(Statement stmt, Connection conn) {
        close(null, stmt, conn);
    }
    
    //Close resource (connection only) 
    public static void close(Connection conn) { 
        close(null, null, conn);
    }

}



    

Tags: JDBC Attribute

Posted on Sun, 31 May 2020 10:36:11 -0700 by benpaxton777