🔍 Code Extractor

function init_connections

Maturity: 29

Initializes and returns a Neo4j database session and driver connection using configuration settings.

File:
/tf/active/vicechatdev/offline_docstore_multi_vice.py
Lines:
46 - 52
Complexity:
simple

Purpose

This function establishes a connection to a Neo4j graph database by reading connection parameters (URI, authentication credentials, and database name) from a config module. It creates both a driver instance for managing the connection and a session object for executing queries. This is typically used as an initialization step before performing any database operations.

Source Code

def init_connections():

    uri = config.DB_ADDR
    user, password = config.DB_AUTH
    driver = GraphDatabase.driver(uri, auth=(user, password))
    session = driver.session(database=config.DB_NAME)
    return session,driver

Return Value

Returns a tuple containing two elements: (session, driver). The 'session' is a Neo4j session object bound to the configured database that can be used to execute Cypher queries. The 'driver' is a Neo4j driver instance that manages the connection pool and can be used to create additional sessions or close the connection. Both objects are required for proper database interaction and cleanup.

Dependencies

  • neo4j
  • config

Required Imports

from neo4j import GraphDatabase
import config

Usage Example

# Ensure config.py has required settings:
# config.DB_ADDR = 'bolt://localhost:7687'
# config.DB_AUTH = ('neo4j', 'password123')
# config.DB_NAME = 'neo4j'

from neo4j import GraphDatabase
import config

def init_connections():
    uri = config.DB_ADDR
    user, password = config.DB_AUTH
    driver = GraphDatabase.driver(uri, auth=(user, password))
    session = driver.session(database=config.DB_NAME)
    return session, driver

# Usage
session, driver = init_connections()
try:
    # Execute queries using session
    result = session.run('MATCH (n) RETURN count(n) as count')
    print(result.single()['count'])
finally:
    # Always close session and driver when done
    session.close()
    driver.close()

Best Practices

  • Always close the session and driver when done using them to prevent resource leaks. Use try-finally blocks or context managers.
  • Store sensitive credentials (DB_AUTH) in environment variables or secure configuration management systems, not in plain text.
  • Consider implementing connection pooling and retry logic for production environments.
  • Handle potential exceptions like ServiceUnavailable and AuthError (imported in the source file) when calling this function.
  • Validate that all required config values (DB_ADDR, DB_AUTH, DB_NAME) are set before calling this function.
  • Consider creating a context manager wrapper around this function for automatic resource cleanup.
  • The function creates a new driver and session each time it's called; consider implementing a singleton pattern or connection pooling for better resource management.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function generate_neo4j_schema_report 49.8% similar

    Generates a comprehensive schema report of a Neo4j graph database, including node labels, relationships, properties, constraints, indexes, and sample data, outputting multiple report formats (JSON, HTML, Python snippets, Cypher examples).

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function generate_python_snippets 46.9% similar

    Generates a Python file containing code snippets and helper functions for interacting with a Neo4j graph database based on the provided schema information.

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function generate_diagram_data 43.8% similar

    Transforms Neo4j schema information into a structured format suitable for graph visualization, creating separate node and edge data structures.

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function generate_cypher_examples 43.6% similar

    Generates a comprehensive Cypher query examples file for interacting with a Neo4j graph database based on the provided schema information.

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function test_filecloud_connection 42.4% similar

    Tests the connection to a FileCloud server by attempting to instantiate a FileCloudClient with credentials from configuration.

    From: /tf/active/vicechatdev/SPFCsync/test_connections.py
← Back to Browse