159

I have a Microsoft SQL server 2005 and I tried to enable Broker for my database with those T-SQL:

 SELECT name, is_broker_enabled FROM sys.databases 
 -- checking its status 0 in my case
 ALTER DATABASE myDatabase SET ENABLE_BROKER

The Alter Database takes long time to process. It is now over half hour and it is still running. Not sure if it is waiting for something else or I have to clean up anything first, such as delete all the messages, contract, queue and services under service broker?

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
  • 1
    As a side note for others, if there are any open connections to the db, they would cause this delay. Run `sp_who` to show if there are any.. (it might be you) – ChipperG Feb 10 '20 at 16:26

4 Answers4

326

http://rusanu.com/2006/01/30/how-long-should-i-expect-alter-databse-set-enable_broker-to-run/

alter database [<dbname>] set enable_broker with rollback immediate;
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
20
USE master;
GO
ALTER DATABASE Database_Name
    SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE;
GO
USE Database_Name;
GO
pgrodrigues
  • 2,083
  • 1
  • 24
  • 28
Irfanullah
  • 217
  • 2
  • 4
5

Enabling SQL Server Service Broker requires a database lock. Stop the SQL Server Agent and then execute the following:

USE master ;
GO

ALTER DATABASE [MyDatabase] SET ENABLE_BROKER ;
GO

Change [MyDatabase] with the name of your database in question and then start SQL Server Agent.

If you want to see all the databases that have Service Broker enabled or disabled, then query sys.databases, for instance:

SELECT
    name, database_id, is_broker_enabled
FROM sys.databases
AKDiscer
  • 151
  • 1
  • 3
  • It worked, I stopped SQL Server Agent and also had to end all db connections (take offline, bring online again) – Adel Mourad Feb 16 '22 at 07:08
2

Actually I am preferring to use NEW_BROKER ,it is working fine on all cases:

ALTER DATABASE [dbname] SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
Mohamad Mahmoud Darwish
  • 3,865
  • 9
  • 51
  • 76