java的DriverManager.getConnection为什么返回同一个connection?

查看:311
本文介绍了java的DriverManager.getConnection为什么返回同一个connection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我启动了十个线程去测试一个业务方法,但有时候会报出连接已经关闭,无法继续操作的异常。直接上代码了:
首先是测试代码:

public class Test {
    public static void main(String[] args){
//        ProductService productService = new ProductServiceImpl();
//        productService.updateProductPrice(1, 3000);

        for(int i=0;i<10;i++){
            ProductService productService = new ProductServiceImpl();
            MyThread myThread = new MyThread(productService);
            myThread.start();
        }
    }
}

class MyThread extends Thread{

    private ProductService productService;

    public MyThread(ProductService productService){
        this.productService = productService;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        productService.updateProductPrice(1,3000);
    }
}

业务代码:

public class ProductServiceImpl implements ProductService{
    private static final String UPDATE_PRODUCT_SQL = "update product set price = ? where id = ?";
    private static final String INSERT_LOG_SQL = "insert into log (created, description) values (?, ?)";

    public void updateProductPrice(long productId, int price) {
        Connection conn = null;
        try {
            // 获取连接
            conn = DBUtil.getConnection();
            System.out.println(Thread.currentThread().getName()+"---->"+conn.toString());
            conn.setAutoCommit(false); // 关闭自动提交事务(开启事务)

            // 执行操作
            updateProduct(conn, UPDATE_PRODUCT_SQL, productId, price); // 更新产品
            insertLog(conn, INSERT_LOG_SQL, "Create product."); // 插入日志

            // 提交事务
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            DBUtil.closeConnection(conn);
        }
    }

    private void updateProduct(Connection conn, String updateProductSQL, long productId, int productPrice) throws Exception {
        PreparedStatement pstmt = conn.prepareStatement(updateProductSQL);
        pstmt.setInt(1, productPrice);
        pstmt.setLong(2, productId);
        int rows = pstmt.executeUpdate();
        if (rows != 0) {
            System.out.println("Update product success!");
        }
    }

    private void insertLog(Connection conn, String insertLogSQL, String logDescription) throws Exception {
        PreparedStatement pstmt = conn.prepareStatement(insertLogSQL);
        pstmt.setString(1, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
        pstmt.setString(2, logDescription);
        int rows = pstmt.executeUpdate();
        if (rows != 0) {
            System.out.println("Insert log success!");
        }
    }
}

数据库操作类:

public class DBUtil {
    private static final String driver = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/demo";
    private static final String username = "root";
    private static final String password = "root";

    // 定义一个数据库连接
    private static Connection conn = null;

    // 获取连接
    public static Connection getConnection() {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    // 关闭连接
    public static void closeConnection(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

其中一次得到的运行结果:

解决方案

问题在DBUtil的这一行代码:

private static Connection conn = null;

conn这个变量是全局静态变量,是所有线程共享的。

一个极端的情况:
线程A执行到conn = DriverManager.getConnection(...) 的时候conn是数据库a连接;
线程B执行到conn = DriverManager.getConnection(...) 的时候conn重新被赋值为数据库b连接;

此时,由于conn是所有线程共享的,对于线程A和线程B来说conn的值是数据库b连接。这就导致了所有的线程共用一个数据库b连接,当一个线程关闭了这个连接,其他的线程也就会抛出异常。

此外,由于最后共享一个数据库b连接,数据库a连接就无法关闭导致内存泄露。

这篇关于java的DriverManager.getConnection为什么返回同一个connection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆