Search code examples
sql-serverservice-broker

Can I turn on service broker on all my databases?


I have a multi-tenant database server with over 200 databases.Can I turn on service broker on all these databases without effecting the performance of the server? Does this have a big impact on the server ?


Solution

  • Activating Service Broker on a database requires exclusive access to the database, in other words all existing connections will need to be terminated and non committed transactions will need to be rolled back. You would need to query sys.databases and set up a Dynamic SQL Statement to loop through the databases to activate Service Broker on all of them. There isn't a server level command to activate Service Broker on all databases.

    Activating Service Broker will not have an impact on the Server until you begin creating Message Types, Contracts, Queues and Services on those Queues and begin accumulating data.

    Here is the code to activate Service Broker, again beware, because it will terminate all connections and rollback all non committed transactions.

    ALTER DATABASE MyDatabase
    SET ENABLE_BROKER 
    WITH ROLLBACK IMMEDIATE 
    GO 
    

    Before activating Service Broker, you can run a quick query to see if it's already been activated.

    SELECT is_broker_enabled FROM sys.databases WHERE name = 'MyDatabase'
    

    This is a great link for a basic Service Broker walkthrough that I found very useful when implementing Service Broker at my last client.

    https://www.simple-talk.com/sql/learn-sql-server/service-broker-foundations-workbench/