🔍 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 get_driver 70.5% similar

    Singleton function that initializes and returns a Neo4j database driver instance with connection verification.

    From: /tf/active/vicechatdev/CDocs/db/__init__.py
  • function initialize_schema 62.7% similar

    Initializes the Neo4j database schema by creating required constraints, indexes, root nodes, audit trails, and migrating existing data structures to current schema versions.

    From: /tf/active/vicechatdev/CDocs/db/schema_manager.py
  • function init_database 60.0% similar

    Initializes a Neo4j database with required schema constraints, creates an AuditTrail node, and migrates existing audit events.

    From: /tf/active/vicechatdev/CDocs/db/__init__.py
  • function close_driver 58.2% similar

    Safely closes a global database driver connection and sets it to None, with error handling and logging.

    From: /tf/active/vicechatdev/CDocs/db/__init__.py
  • function get_system_configuration 54.9% similar

    Retrieves system configuration settings from environment variables for display in an admin panel, including database connections, authentication settings, and operational parameters.

    From: /tf/active/vicechatdev/CDocs/controllers/admin_controller.py
← Back to Browse