/* * Copyright (c) 2019. 黄钰朝 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.hyc.wechat.test; import com.hyc.wechat.dao.DataSource; import com.hyc.wechat.dao.impl.DataSourceImpl; import com.hyc.wechat.model.po.User; import com.hyc.wechat.model.po.abs.BaseEntity; import com.hyc.wechat.util.JdbcUtils; import com.mysql.cj.x.protobuf.MysqlxDatatypes; import java.sql.Connection; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; /** * @author 黄钰朝 * @program wechat * @description 测试数据源,用于验证数据源连接的获取和释放功能是否正常工作 * @date 2019-05-01 16:49 */ public class TestDataSource { public static void main(String[] args) throws SQLException { // 输出测试开始信息 System.out.println("测试直接从数据源获取连接"); // 获取数据源的单例实例 DataSource dataSource = DataSourceImpl.getInstance(); // 从数据源获取一个数据库连接 Connection conn = dataSource.getConnection(); // 输出获取到的连接信息 System.out.println("获取一个连接\n"); System.out.println(conn); // 输出当前已创建的连接数 System.out.println("当前已创建连接数 = " + dataSource.getCurrentCount()); // 输出当前空闲的连接数 System.out.println("当前空闲连接数 = " + dataSource.getfreeCount()); // 释放刚才获取的连接 System.out.println("释放一个连接"); dataSource.freeConnection(conn); // 循环获取和释放连接10次 for (int i = 0; i < 10; i++) { conn = dataSource.getConnection(); System.out.println(conn); // 使用工具类关闭连接 JdbcUtils.close(conn); } // 注意:这里再次释放连接可能会导致空指针异常,因为conn在循环中已经被关闭 System.out.println("释放一个连接"); dataSource.freeConnection(conn); // 输出当前已创建的连接数 System.out.println("当前已创建连接数 = " + dataSource.getCurrentCount()); // 输出当前空闲的连接数 System.out.println("当前空闲连接数 = " + dataSource.getfreeCount()); } }