Skip to main content

Disconnect

Closes an existing Microsoft SQL Server database connection.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.

Inputs

  • Connection Id - The connection identifier returned by the Connect node. This identifies which connection to close.

Options

  • None

Output

  • None - The connection is closed, but no results are returned.

How It Works

The Disconnect node closes an active SQL Server database connection. When executed, the node:

  1. Validates the connection ID is provided and not empty
  2. Retrieves the connection from the internal connections dictionary
  3. Removes the connection from the dictionary
  4. Closes the SQL Server connection
  5. Releases all resources associated with the connection

Requirements

  • Valid connection ID from a Connect node
  • The connection must not have been previously closed

Error Handling

The node will return specific errors in the following cases:

  • ErrInvalidArg - Connection ID is empty or not provided
  • ErrNotFound - Connection not found (already closed or invalid ID)

Usage Examples

Basic Disconnect

Connect (output: conn_id)

Query (use conn_id)

Non Query (use conn_id)

Disconnect (use conn_id)
// The connection ID flows through the automation
// No manual intervention needed if using message context

Multiple Operations Before Disconnect

Connect

Query 1 (fetch users)

Query 2 (fetch orders)

Insert (save results)

Disconnect

Disconnect in Error Handler

Try:
Connect

Query

Non Query
Catch:
Log Error
Finally:
Disconnect (ensures connection is closed even if error occurs)

Common Use Cases

  1. Resource Cleanup - Free database connections when automation completes
  2. Connection Management - Close connections between batch operations
  3. Error Recovery - Ensure connections are closed even when errors occur
  4. Performance Optimization - Release connections to reduce server load
  5. Flow Completion - Clean disconnect at the end of database workflows
  6. Connection Pool Management - Return connections to available pool

Tips for Effective Use

  • Always Disconnect - Always close connections when finished to prevent resource leaks
  • Use Finally Blocks - Place Disconnect in Finally nodes to ensure execution even on errors
  • One Connection, One Disconnect - Each Connect node should have a corresponding Disconnect
  • Connection ID Management - Ensure the same connection ID is used throughout the flow
  • Timing - Disconnect only after all database operations using that connection are complete
  • Error Handling - Don't let errors prevent disconnection; use Try-Catch-Finally pattern

Best Practices

Pattern 1: Simple Flow

Connect → Operations → Disconnect

Pattern 2: Error Handling

Try:
Connect
Database Operations
Catch:
Handle Error
Finally:
Disconnect (always executes)

Pattern 3: Multiple Connections

Connect (DB1) → conn_id_1
Connect (DB2) → conn_id_2

Operations using both connections

Disconnect (conn_id_1)
Disconnect (conn_id_2)

Pattern 4: Long-Running Flow

Connect

Loop:
Query
Process Data
Insert Results

Disconnect (after loop completes)

Common Errors and Solutions

Connection ID Cannot Be Empty

Error: ErrInvalidArg: Connection ID cannot be empty

Solutions:

  • Ensure the Connect node runs before Disconnect
  • Verify the connection ID variable is correctly passed
  • Check that the connection ID output variable name matches the input

Connection Not Found

Error: ErrNotFound: Connection not found

Solutions:

  • Verify the Connect node executed successfully
  • Check that the connection wasn't already disconnected
  • Ensure the connection ID is correct and hasn't been modified
  • Verify the connection wasn't closed by a timeout or error

Connection Already Closed

Symptom: Error occurs when trying to disconnect an already-closed connection

Solutions:

  • Use ContinueOnError property if connection might already be closed
  • Check connection state before disconnecting (use Try-Catch)
  • Ensure only one Disconnect node per connection
  • Don't call Disconnect multiple times for the same connection ID

Resource Management

Why Disconnecting is Important

Resource Leaks:

  • Unclosed connections consume server memory
  • Each connection uses network resources
  • SQL Server has maximum connection limits

Performance Impact:

  • Too many open connections slow down SQL Server
  • Connection pool exhaustion can block new connections
  • Unused connections waste server resources

Best Practice:

✓ GOOD: Connect → Use → Disconnect
✗ BAD: Connect → Use (no disconnect)

Connection Lifecycle

  1. Connect - Opens connection, returns ID
  2. Active - Connection is used for queries/operations
  3. Disconnect - Connection is closed and resources freed
  4. Disposed - Connection removed from memory

Advanced Scenarios

Conditional Disconnect

// JavaScript node before Disconnect
if ($item.shouldDisconnect !== false) {
// Continue to Disconnect node
return $item;
}
// Skip Disconnect if condition not met

Disconnect with Logging

Query/Operations

JavaScript: Log connection stats

Disconnect

JavaScript: Log successful disconnect

Multiple Database Pattern

// Connect to multiple databases
// Use different connection IDs

// Source database
const sourceConnId = $item.source_conn_id;

// Target database
const targetConnId = $item.target_conn_id;

// Disconnect both at the end

Transaction with Disconnect

Connect

Start Transaction

Operations (within transaction)

Commit Transaction

Disconnect

Troubleshooting

Orphaned Connections

Problem: Connections not properly closed due to flow errors

Solution:

  • Always use Try-Catch-Finally pattern
  • Place Disconnect in Finally block
  • Monitor SQL Server connection count
  • Implement connection timeout policies

Connection Timeout

Problem: Connection times out before Disconnect is called

Solution:

  • Reduce time between operations
  • Check for long-running queries
  • Ensure flow completes in reasonable time
  • Handle timeouts gracefully

Multiple Disconnect Attempts

Problem: Same connection ID used in multiple Disconnect nodes

Solution:

  • Use each connection ID only once for Disconnect
  • Implement connection tracking in your flow
  • Use status variables to track disconnection state

Monitoring and Debugging

Log Connection Events

// Before Disconnect
console.log(`Disconnecting from database: ${$item.conn_id}`);
console.log(`Operations completed: ${$item.operationCount}`);

// After Disconnect (in next node)
console.log(`Successfully disconnected from database`);

Connection Duration Tracking

// In Connect node output
const connectTime = new Date();

// Before Disconnect
const disconnectTime = new Date();
const duration = disconnectTime - connectTime;
console.log(`Connection active for ${duration}ms`);

See Also

  • Connect - Establish database connections
  • Query - Execute SELECT queries
  • Non Query - Execute INSERT, UPDATE, DELETE
  • Commit - Commit transactions