{"id":26241101,"url":"https://github.com/abrja/connectionpools.jl","last_synced_at":"2026-04-25T22:33:35.122Z","repository":{"id":279005181,"uuid":"936329844","full_name":"AbrJA/ConnectionPools.jl","owner":"AbrJA","description":"Julia package to manage Pool of objects of any Type mainly focus on database connections.","archived":false,"fork":false,"pushed_at":"2025-03-12T07:32:43.000Z","size":253,"stargazers_count":0,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-12T08:29:07.166Z","etag":null,"topics":["connector","database","database-connector","julia-language","julia-package","pool","thread-safe"],"latest_commit_sha":null,"homepage":"https://abrja.github.io/ConnectionPools.jl/","language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AbrJA.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-20T22:48:40.000Z","updated_at":"2025-03-12T07:49:50.000Z","dependencies_parsed_at":"2025-02-23T06:13:28.420Z","dependency_job_id":"aff2ad4c-3f7e-47b8-83a6-136ed15c0c53","html_url":"https://github.com/AbrJA/ConnectionPools.jl","commit_stats":null,"previous_names":["abrja/pools.jl","abrja/connectionpools.jl"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbrJA%2FConnectionPools.jl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbrJA%2FConnectionPools.jl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbrJA%2FConnectionPools.jl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbrJA%2FConnectionPools.jl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AbrJA","download_url":"https://codeload.github.com/AbrJA/ConnectionPools.jl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243365824,"owners_count":20279251,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["connector","database","database-connector","julia-language","julia-package","pool","thread-safe"],"created_at":"2025-03-13T08:19:16.069Z","updated_at":"2026-04-25T22:33:35.117Z","avatar_url":"https://github.com/AbrJA.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ConnectionPools\n\n[![Build Status](https://github.com/AbrJA/Pools.jl/workflows/CI/badge.svg)](https://github.com/AbrJA/ConnectionPools.jl/actions)\n\n## Install\n\n```julia\nimport Pkg; Pkg.add(\"ConnectionPools\")\n```\n\n## Goal\n\n**Pool** is a collection of reusable resources that can be efficiently managed and allocated to avoid the overhead of creating and destroying them repeatedly. This package is built to manage `Pool` of objects of any `Type` mainly focus on database connections.\n\nIt relies on the custom implementation of the following functions:\n\n```julia\ncreate(::Type{T}) # How to create the resource\ncheck(::T) # How to check it\nchange!(::T) # How to update it\nclean!(::T) # How to finalize it\n```\n\nAt least `create` function is required.\n\n## Features\n\n- **Generic:**  Works with any resource type `T`.  You define how to manage resources, and `ConnectionPools.jl` handles the rest.\n- **Thread-safe:** All operations are thread-safe, allowing concurrent access to the pool from multiple tasks.\n- **Memory-safe:** Handles resource allocation, and deallocation, limiting the number of resources in use concurrently.\n- **Convenient:** Function `withconnection` simplifies the process of acquiring and using resources.\n\n## Examples\n\n### SQLite\n\nTo create a `ConnectionPool` of `SQLite` connections:\n\n```julia\nusing DBInterface, DataFrames, SQLite\n\n# Connect to SQLite database\ndb = SQLite.DB(\"database.db\")\n\n# Create table if it doesn't exist\nDBInterface.execute(db, \"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)\")\n\n# Begin a transaction to speed up batch inserts\nDBInterface.execute(db, \"BEGIN TRANSACTION\")\n\n# Insert 1000 records into the users table\nfor i in 1:1000\n    name = \"User$i\"\n    age = rand(20:60)  # Random age between 20 and 60\n    DBInterface.execute(db, \"INSERT INTO users (name, age) VALUES (?, ?)\", (name, age,))\nend\n\n# Commit transaction\nDBInterface.execute(db, \"COMMIT\")\n\n# Close database connection\nDBInterface.close!(db)\n```\n\n- Load the libraries\n```julia\nusing ConnectionPools, DBInterface, DataFrames, SQLite\n```\n\n- Import the functions from `ConnectionPools` to be extended (just those needed):\n```julia\nimport ConnectionPools: create, clean!\n```\n\n- Implement the required functions:\n```julia\ncreate(::Type{SQLite.DB}) = SQLite.DB(\"database.db\")\nclean!(db::SQLite.DB) = DBInterface.close!(db)\n```\n\n- Create a `ConnectionPool` of `SQLite.DB` with a limit of 5:\n```julia\npool = ConnectionPool{SQLite.DB}(5)\n```\n\n- Use the connections from the pool (using withconnection is recommended):\n```julia\n@time Threads.@threads for i in 1:20\n    withconnection(pool) do db\n        df = DBInterface.execute(db, \"SELECT * FROM users LIMIT $i\") |\u003e DataFrame\n        @info \"Thread $(Threads.threadid()) - Number of rows: $(nrow(df))\"\n    end\nend\n```\n\n- Drain the pool (release and finalize all resources):\n```julia\ndrain!(pool)\n```\n\n### Redis\n\nTo create a `ConnectionPool` of `Redis` connections:\n\n- Load the libraries:\n```julia\nusing ConnectionPools, Dates, Redis\n```\n\n- Import the functions from `ConnectionPools` to be extended:\n```julia\nimport ConnectionPools: create, check, change!, clean!\n```\n\n- Build the `Resource` struct:\n```julia\nmutable struct Resource\n    conn::RedisConnection\n    timestamp::DateTime\nend\n```\n\n- Implement the required functions for `Type` `Resource`:\n```julia\ncreate(::Type{Resource}) = Resource(RedisConnection(host = \"localhost\", port = 6379, db = 3), now())\ncheck(resource::Resource) = if now() \u003e resource.timestamp + Minute(1) ping(resource.conn) end\nchange!(resource::Resource) = resource.timestamp = now()\nclean!(resource::Resource) = disconnect(resource.conn)\n```\n\n- Create a `ConnectionPool` of `Connection`s with a limit of 5:\n```julia\npool = ConnectionPool{Resource}(5)\n```\n\n- Use a connection from the pool:\n```julia\nwithconnection(pool) do resource\n    # ... use the connection ...\n    get(resource.conn, \"key\")\nend # The connection is automatically released back to the pool here\n```\n\n- Acquire and release manually (less recommended):\n```julia\nresource = acquire!(pool)\ntry\n    get(resource.conn, \"key\")\nfinally\n    release!(pool, resource)\nend\n```\n\n- Drain the pool:\n```julia\ndrain!(pool)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabrja%2Fconnectionpools.jl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabrja%2Fconnectionpools.jl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabrja%2Fconnectionpools.jl/lists"}