Implementation:Apache Shardingsphere DatabaseListenerChangedHandler Handle
| Knowledge Sources | |
|---|---|
| Domains | Cluster, Database_Lifecycle |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
DatabaseListenerChangedHandler manages database creation and dropping in cluster mode by coordinating event watching, metadata updates, and statistics refresh.
Description
DatabaseListenerChangedHandler implements GlobalDataChangedEventHandler and subscribes to DatabaseListenerCoordinatorNodePath for ADDED and UPDATED events. When a database coordination event is received, it extracts the database name and action type (CREATE or DROP) from the event. For CREATE, it registers a DatabaseMetaDataChangedListener watcher on the database metadata path and adds the database to the metadata context. For DROP, it removes the data listener and drops the database from the metadata context. After either operation, it cleans up the coordinator path and triggers an asynchronous statistics refresh if the instance type is PROXY.
Usage
This handler is auto-loaded via SPI in cluster mode. It coordinates database lifecycle events across cluster nodes, ensuring all instances register or deregister watchers and update their local metadata consistently when databases are created or dropped.
Code Reference
Source Location
- Repository: Apache_Shardingsphere
- File: DatabaseListenerChangedHandler.java
- Lines: 1-80
Signature
public final class DatabaseListenerChangedHandler implements GlobalDataChangedEventHandler {
public NodePath getSubscribedNodePath()
public Collection<Type> getSubscribedTypes()
public void handle(final ContextManager contextManager, final DataChangedEvent event)
}
Import
import org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.global.state.DatabaseListenerChangedHandler;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| contextManager | ContextManager | Yes | Provides access to metadata context, persist facade, and instance context |
| event | DataChangedEvent | Yes | Contains the coordinator path key and action value (CREATE/DROP) |
Outputs
| Name | Type | Description |
|---|---|---|
| void | void | Side effects: registers/deregisters watchers, adds/drops database, refreshes statistics |
Usage Examples
// This handler is triggered when a database coordinator event is persisted:
// On database creation, a watcher is added for the new database path
// and the database is added to the metadata context.
// On database drop, the watcher is removed and the database is dropped.
// Statistics are asynchronously refreshed on PROXY instances.