实现正确的JDBC连接池

时间:2016-07-02 12:30:09

标签: java jdbc connection-pooling

我有多个类用于为我的表创建DAO对象。他们每个都有自己的连接对象,如果我正确实现连接池,我会有点困惑。

似乎每次我创建一个DAO对象时,JDBC都会给我一个新的连接,但不会给我之前关闭的那个连接。如何实现正确的线程安全连接池?

public class JDBCTabletDAO implements TabletDAO {
static {
    new DOMConfigurator().doConfigure("EPAM6 Final Task\\properties\\log4j.xml", org.apache.log4j.LogManager.getLoggerRepository());
}

private static final Logger log = Logger.getLogger(String.valueOf(JDBCTabletDAO.class));
/**
 * connection for interacting with sql
 */
private Connection connection;
/**
 * data source for specified schema
 */
private DataSource dataSource;

/**
 * gets the connection from connection pool
 * @throws NamingException
 * @throws SQLException
 */
public JDBCTabletDAO() throws NamingException, SQLException{
    Context envCtx = new InitialContext();
    dataSource = (DataSource)envCtx.lookup("java:comp/env/jdbc/pharmacy");
    connection = dataSource.getConnection();
}

@Override
public void delete(int tabletId) {
    PreparedStatement preparedStatement = null;
    try{
        if(connection.isClosed())
            connection = dataSource.getConnection();
        preparedStatement = connection.prepareStatement("DELETE FROM tablets WHERE idTablet=?");
        preparedStatement.setInt(1, tabletId);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        DbUtil.close(preparedStatement);
        DbUtil.close(connection);
    }
}

@Override
public void update(Tablet tablet) {
    PreparedStatement preparedStatement = null;
    try{
        if(connection.isClosed())
            connection = dataSource.getConnection();
        preparedStatement = connection.prepareStatement("UPDATE tablets SET name=?," +
                "price=?, type_id=?, description=?, need_recipe=?, weight_of_pack=?, pills_count=? WHERE idTablet=?;");
        preparedStatement.setString(1, tablet.getName());
        preparedStatement.setDouble(2, tablet.getPrice());
        preparedStatement.setInt(3, tablet.getTypeId());
        preparedStatement.setString(4, tablet.getDescription());
        preparedStatement.setBoolean(5, tablet.isNeedRecepie());
        preparedStatement.setDouble(6, tablet.getWeight());
        preparedStatement.setInt(7, tablet.getPillsCount());
        preparedStatement.setInt(8, tablet.getTabletId());

        preparedStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        DbUtil.close(preparedStatement);
        DbUtil.close(connection);
    }
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" crossContext="true" reloadable="true" path="patient/account">
    <Resource
            name="jdbc/pharmacy"
            author="Container"
            type="javax.sql.DataSource"
            maxActive="64"
            maxIdle="8"
            maxWait="10000"
            username="root"
            password="blablabla"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/pharmacy"  />
</Context>

0 个答案:

没有答案