package com.qiwu.chatbotbackend.db; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.log4j.Logger; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class C3p0Utils { static Logger logger = Logger.getLogger(C3p0Utils.class.getName()); //通过标识名来创建相应连接池 static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql"); //通过标识名来创建相应连接池 static ComboPooledDataSource dataSource2 = new ComboPooledDataSource("engine"); static ComboPooledDataSource dataSource3 = new ComboPooledDataSource("platform"); //从连接池中取用一个连接 public static Connection getConnection() throws SQLException { try { return dataSource.getConnection(); } catch (Exception e) { logger.error("Exception in C3p0Utils!", e); throw e; } } //从连接池中取用一个连接 public static Connection getEngineConnection() throws SQLException { try { return dataSource2.getConnection(); } catch (Exception e) { logger.error("Exception in C3p0Utils!", e); throw e; } } //从连接池中取用一个连接 public static Connection getPlatformConnection() throws SQLException { try { return dataSource3.getConnection(); } catch (Exception e) { logger.error("Exception in C3p0Utils!", e); throw e; } } //释放连接回连接池 public static void close(Connection conn, PreparedStatement pst, ResultSet rs) throws SQLException { if (rs != null) { try { rs.close(); } catch (SQLException e) { logger.error("Exception in C3p0Utils!", e); throw e; } } if (pst != null) { try { pst.close(); } catch (SQLException e) { logger.error("Exception in C3p0Utils!", e); throw e; } } if (conn != null) { try { conn.close(); } catch (SQLException e) { logger.error("Exception in C3p0Utils!", e); throw e; } } } }