SqlLite数据库连接池

简介: 这篇文章介绍了如何使用HikariCP作为SQLite数据库的连接池,包括配置依赖、创建连接池、获取连接、关闭连接以及进行数据库操作的示例代码和性能测试对比。

)1、使用数据库连接池过程

1.1 引入pom

        <!--HikariCP数据库连接池-->
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>3.4.5</version>
        </dependency>

1.2 创建数据库连接池

    public final static String DB_TRANSPATH ="D:\\GrgBanking\\iTerminal\\" + "Data" + File.separator + "trans.db";
    private static String Drivde = "org.sqlite.JDBC";
    private static Statement statement;
    private static Connection connection;
    private static ResultSet resultSet;
    /**
     * 4.提升作用域
     */
    private static HikariDataSource dataSource;
    private static HikariConfig config;

    //创建日志对象 Logger
    static final Logger log = Logger.getLogger(JdbcUtils_HKCP.class);

    /**
     * 1.获取数据源 dataSource
     */
    static {
        try {

            log.info("开始创建数据库连接池");
            config = new HikariConfig();
            String dbFilePath = "jdbc:sqlite:" + DB_TRANSPATH;
            config.setJdbcUrl(dbFilePath);
            config.setDriverClassName(Drivde);
            dataSource = new HikariDataSource(config);
        } catch (Exception e) {
            log.error("创建数据库连接池失败:", e);
        }
        log.info("创建数据库连接池结束");
    }

1.3 获取连接 (数据源dataSource自带连接)

    /**
     * 2.获取连接 (数据源dataSource自带连接)
     *
     * @return
     * @throws SQLException
     * @throws SQLException
     */
    public void connect() {
        log.info("SqlLite connection start");
        try {
            if (null == connection) {
                connection = dataSource.getConnection();
                statement = connection.createStatement();
            }
        } catch (SQLException e) {
            log.error("SqlLite connection error:", e);
        }
        log.info("SqlLite connection end");

    }

1.4 关闭连接

    /**
     * @param
     * @return
     * @author ZYZ
     * @description 3.释放数据库资源
     */
    public static void release() {
        log.info("SqlLite close start");
        try {
            if (null != resultSet) {
                resultSet.close();
                resultSet = null;
            }
            if (null != statement) {
                statement.close();
                statement = null;
            }
            if (null != connection) {
                connection.close();
                connection = null;
            }
        } catch (SQLException e) {
            log.error("SqlLite close error", e);
        }
        log.info("SqlLite close end");

    }

1.5 增加案例

    /**
     * @param sql sql语句
     * @return int
     * @author ZYZ
     * @description 插入数据库操作并返回数据ID
     */
    public int insertGoBackIDHKCP(String sql) throws SQLException {
        try {
            connect();
            log.info("insertGoBackID SQL:" + sql);
            statement.executeUpdate(sql);
            resultSet = statement.getGeneratedKeys();
            int iresult = resultSet.getInt(1);
            return iresult;
        } finally {
            release();
        }
    }

1.6 测试

 ElectronicFlowUtils.insert(seriaNumber, "测试数据库连接池");
2023-11-28 19:20:48  INFO (com.zyz.yu.SRCSqLite.helper.JdbcUtils_HKCP:42) - 开始创建数据库连接池
2023-11-28 19:20:48 DEBUG (com.zaxxer.hikari.HikariConfig:946) - Driver class org.sqlite.JDBC found in Thread context class loader sun.misc.Launcher$AppClassLoader@58644d46
2023-11-28 19:20:48 DEBUG (com.zaxxer.hikari.HikariConfig:1066) - HikariPool-1 - configuration:
2023-11-28 19:20:48 DEBUG (com.zaxxer.hikari.HikariConfig:1098) - allowPoolSuspension.............false
2023-11-28 19:20:48 DEBUG (com.zaxxer.hikari.HikariConfig:1098) - autoCommit......................true
2023-11-28 19:20:48 DEBUG (com.zaxxer.hikari.HikariConfig:1098) - catalog.........................none
  ### 中间省略了大部分数据库连接池的日志打印
2023-11-28 19:20:49  INFO (com.zaxxer.hikari.HikariDataSource:80) - HikariPool-1 - Starting...
2023-11-28 19:20:49 DEBUG (com.zaxxer.hikari.pool.HikariPool:564) - HikariPool-1 - Added connection org.sqlite.jdbc4.JDBC4Connection@6325a3ee
2023-11-28 19:20:49  INFO (com.zaxxer.hikari.HikariDataSource:82) - HikariPool-1 - Start completed.
2023-11-28 19:20:49  INFO (com.zyz.yu.SRCSqLite.helper.JdbcUtils_HKCP:51) - 创建数据库连接池结束
2023-11-28 19:20:49  INFO (com.zyz.yu.SRCSqLite.helper.JdbcUtils_HKCP:62) - SqlLite connection start
2023-11-28 19:20:49  INFO (com.zyz.yu.SRCSqLite.helper.JdbcUtils_HKCP:71) - SqlLite connection end
2023-11-28 19:20:49  INFO (com.zyz.yu.SRCSqLite.helper.JdbcUtils_HKCP:85) - insertGoBackID SQL:insert into electronic_flow(serial_number,content,create_time,update_time,upload_flag,description,res_code) values('602120','测试数据库连接池','2023-11-28 19:20:49','',0,'','');
2023-11-28 19:20:49  INFO (com.zyz.yu.SRCSqLite.helper.JdbcUtils_HKCP:140) - SqlLite close start
2023-11-28 19:20:49  INFO (com.zyz.yu.SRCSqLite.helper.JdbcUtils_HKCP:157) - SqlLite close end

简单测试 对比了一下 使用数据库连接池 和 不适用情况下的时间效率。

```
/**

 * 测试插入数据库,采用数据池的方式
 *              //插入 50次 执行时长 957毫秒
 *              //插入100次 执行时长:1077 毫秒.
 *              // 插入 1000 次 执行时长:3309 毫秒.
 *              // 执行1000次 查询 和 添加 执行时长:3916 毫秒.
 *              插入 1000次 查询 1000 次 修改 1000次 执行时长:4924 毫秒. 执行时长:5180 毫秒.
 *
 */

/**
 * 测试插入数据库,不使用数据池技术
 *              //插入 50次 执行时长 执行时长:1011 毫秒.
 *              //插入100次 执行时长:1167 毫秒.
 *              //插入1000次  执行时长:3931 毫秒.
 *              插入 1000次 查询 1000 次 执行时长:5199 毫秒.
 *             插入 1000次 查询 1000 次 修改 1000次 执行时长:6508 毫秒.
 *
 */
相关文章
|
10月前
|
存储 设计模式 Cloud Native
C++QT SqlLite数据库简单使用
C++QT SqlLite数据库简单使用
|
22天前
|
Java 数据库连接 数据库
sqlLite 如何使用数据库连接池
这篇文章介绍了如何在SQLite数据库操作中使用HikariCP连接池以减少频繁建立和释放数据库连接的资源消耗,包括在Maven项目中添加依赖、配置HikariDataSource对象以及实现数据库连接池的具体代码示例。
|
2月前
|
SQL 关系型数据库 MySQL
|
SQL Dart 算法
Flutter SqlLite数据库快速入门
Flutter SqlLite数据库快速入门
Flutter SqlLite数据库快速入门
|
数据库 Python
Python操作sqllite数据库
Python操作sqllite数据库
146 0
|
存储 数据库 Python
使用python获取历史天气,存储到sqllite数据库
使用python获取历史天气,存储到sqllite数据库
179 0
|
数据库 Android开发
Android中SQLlite数据库的增删改查
Android中SQLlite数据库的增删改查
200 0
|
SQL 缓存 测试技术
07_Android操作sqllite数据库(包括2中方式操作数据的方式),单元测试,BaseAdapter的使用,自定义view的综合使用案例
 1 目标从sqllite中读取数据并显示如下: MainActivity对应的界面 MainActivity2对应的界面           2  配置Android的清单文件 &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xm
1230 0
|
SQL 数据库 数据安全/隐私保护
初识SqlLite ---.net连接数据库
  Sqlite 是一款轻量级的关系型数据库,以小巧和嵌入式闻名。以前只是听说,现在终于忍不住要尝试下.本文的初衷是为.net平台的使用者提供帮助。 Sqlite有专门为VS2010开发的程序包,大家可以到System.
1506 0
|
21天前
|
SQL 关系型数据库 MySQL
【揭秘】MySQL binlog日志与GTID:如何让数据库备份恢复变得轻松简单?
【8月更文挑战第22天】MySQL的binlog日志记录数据变更,用于恢复、复制和点恢复;GTID为每笔事务分配唯一ID,简化复制和恢复流程。开启binlog和GTID后,可通过`mysqldump`进行逻辑备份,包含binlog位置信息,或用`xtrabackup`做物理备份。恢复时,使用`mysql`命令执行备份文件,或通过`innobackupex`恢复物理备份。GTID模式下的主从复制配置更简便。
91 2