Neo4j.Connection.Pool (Neo4jEx v0.1.4)

View Source

Connection pool for Neo4j drivers using poolboy.

This module provides connection pooling functionality to improve performance and resource management when working with Neo4j databases.

Usage

# Start a connection pool
{:ok, _pool} = Neo4j.Connection.Pool.start_pool([
  uri: "bolt://localhost:7687",
  auth: {"neo4j", "password"},
  pool_size: 15,
  max_overflow: 5
])

# Execute queries using the pool
{:ok, results} = Neo4j.Connection.Pool.run("MATCH (n:Person) RETURN n")

# Execute transactions using the pool
Neo4j.Connection.Pool.transaction(fn ->
  Neo4j.Connection.Pool.run("CREATE (p:Person {name: 'Alice'})")
  Neo4j.Connection.Pool.run("CREATE (p:Person {name: 'Bob'})")
end)

Summary

Functions

Return a connection to the pool.

Get a connection from the pool.

Execute a query using a pooled connection.

Start a connection pool.

Get pool status information.

Stop a connection pool.

Execute a function within a transaction using a pooled connection.

Functions

checkin(pool_name \\ __MODULE__, worker)

Return a connection to the pool.

Parameters

  • pool_name - Pool name (default: Elixir.Neo4j.Connection.Pool)
  • worker - Connection worker PID

checkout(pool_name \\ __MODULE__, timeout \\ 5000)

Get a connection from the pool.

Parameters

  • pool_name - Pool name (default: Elixir.Neo4j.Connection.Pool)
  • timeout - Checkout timeout in milliseconds (default: 5000)

Returns

  • Connection worker PID

run(query, params \\ %{}, opts \\ [])

Execute a query using a pooled connection.

Parameters

  • query - Cypher query string
  • params - Query parameters map (default: %{})
  • opts - Query options (default: [])

Options

  • :pool_name - Pool name (default: Elixir.Neo4j.Connection.Pool)
  • :timeout - Query timeout in milliseconds

Returns

  • {:ok, results} on success
  • {:error, reason} on failure

Examples

{:ok, results} = Neo4j.Connection.Pool.run("MATCH (n:Person) RETURN n")
{:ok, results} = Neo4j.Connection.Pool.run("CREATE (p:Person {name: $name})", %{name: "Alice"})

start_pool(opts)

Start a connection pool.

Options

  • :uri - Neo4j connection URI (required)
  • :auth - Authentication tuple {username, password} or map
  • :pool_size - Maximum number of connections (default: 10)
  • :max_overflow - Maximum overflow connections (default: 5)
  • :user_agent - Client user agent string
  • :connection_timeout - Connection timeout in milliseconds
  • :query_timeout - Query timeout in milliseconds
  • :name - Pool name (optional)

Examples

{:ok, _pool} = Neo4j.Connection.Pool.start_pool([
  uri: "bolt://localhost:7687",
  auth: {"neo4j", "password"},
  pool_size: 15,
  max_overflow: 5
])

status(pool_name \\ __MODULE__)

Get pool status information.

Parameters

  • pool_name - Pool name (default: Elixir.Neo4j.Connection.Pool)

Returns

Map with pool status information

stop_pool(pool_name \\ __MODULE__)

Stop a connection pool.

Parameters

  • pool_name - Pool name (default: Elixir.Neo4j.Connection.Pool)

transaction(fun, opts \\ [])

Execute a function within a transaction using a pooled connection.

Parameters

  • fun - Function to execute within the transaction
  • opts - Transaction options (default: [])

Options

  • :pool_name - Pool name (default: Elixir.Neo4j.Connection.Pool)
  • :timeout - Transaction timeout in milliseconds

Returns

Result of the function

Examples

Neo4j.Connection.Pool.transaction(fn ->
  Neo4j.Connection.Pool.run("CREATE (p:Person {name: 'Alice'})")
  Neo4j.Connection.Pool.run("CREATE (p:Person {name: 'Bob'})")
end)