How can I write a query for notification request from application to Database? - sql-server

This is my perivous question. about how to push the data from database instead of application pull data.
After went through the link and the documents I come to know that I need to write a query in application side for notification request then the DB will register my request in subscription and If there is any change in the DB it will check subscription, if the request is there it will send the message to the client.
This para is from the above mentioned link
Along with the query submitted for the notification, the client submits a service name
and a broker instance. Each notification subscription begins a Service Broker dialog
with this provided service and broker instance. When data is changed and the change
would affect the result of the submitted query result, a message is sent on this
dialog. By sending this message, the client is considered notified and the notification
subscription is removed. If client desires to be notified on further changes, is
supposed to subscribe again.
Now I don't know how to write a query in application side for notification request and I don't under stand submit a service and a broker instance from the above given para. I guess the service and broker instance needs to be created in application before query the database for notification request. If it is so how to create service and a broker instance?
I can check the DB server for subscription by select * from sys.dm_qn_subscriptions command. can anyone help me?

Probably you didn't understand answer to your previous question completely, so I will try to clarify some things:
I guess the service and broker instance needs to be created in
application before query the database for notification request. If it
is so how to create service and a broker instance?
Service broker is a component of SQL Server, you don't instance nor create it. You enable it - it is a SQL Server admin task to do it.
To create a new service you have to first create queue for it and a contract to have a way of communication. Then you create your service.
When you create your service, instancing, activation and other related operations are handled by the Service Broker.

Related

SQL Service Broker - Send Message Between databases with different Authorizations

I'm trying to figure out how to set up the proper security to send a message from one database to another. The issue I'm having is that the sending database service is bound to a specific user via the authorization, because that service also sends messages to a remote service binding.
So here's my service:
CREATE SERVICE [WebRequestService]
AUTHORIZATION WebDbSSBUser
ON QUEUE [dbo].[WebRequestQueue]
([RequestContract],[CountRequestContract],[OrderRequestContract]);
GO
I want to send a message from that service to another service in a different local database as well. That service is:
CREATE SERVICE [AppRequestService]
AUTHORIZATION DBO
ON QUEUE [dbo].AppRequestQueue
([RequestContract]);
AppRequestService does not have to send messages to any remote bindings, so it's authorized by DBO. When I try to send:
declare #handle uniqueidentifier
begin dialog conversation #handle from service [WebRequestService] to service 'AppRequestService' on contract [RequestContract];
send on conversation #handle message type [AppRequest] (#item_data)
I get this in the transmission log:
An exception occurred while enqueueing a message in the target queue.
Error: 916, State: 3. The server principal
"S-1-9-3-1771139253-1243890769-2689325230-2851569174" is not able to
access the database "appmembership" under the current security
context.
The sid is WebDbSSBUser.
Not sure how I should set up the permissions and I'd prefer to not have to set up a separate queue.
use appmembership
go
create user WebDbSSBUser --for login WebDbSSBUser
go
grant send on service::AppRequestService to WebDbSSBUser
go

Temp XML Schema Collection is automatically created

I recently enabled Service Broker on my server and created a XML Schema Collection to validate the message.
Now I'm receiving alerts from my audit alert as the following
Looking at the Audit log I can see this change was made with my user (sysadmin) on the tempdb database.
I need to worry about this? Why this is happening somethimes?
If I don't need to worry, can I turn off only this alert whitout loose the rest of alerts for changes?
As always ¡Thanks!

Setting up database access

Which is the better way to set up access on some databases that my web apps query. I can only think of one pro for one and one con for the other, so I need some other input before making a final decision.
Option 1 – 10 apps, 10 databases, 1 Service Accounts for each app (User does not have direct access)
All query requests go through the Service Account to the database. The con I can think of is that there is no record of who sent the request, just that the SA accessed the db with a request.
Option 2 – 10 apps, 10 databases, User has direct access (no Service Account)
All query requests from the app go directly to the db and each request is logged, identifying who sent the request from what app. This setup could be locked down further by allowing the specific app access only to db/tables/columns that it needs to complete the request/query. The obvious pro is there would be no anonymous requests; all requests could be traced back to the requester and not just to a SA.
If by Service Account you refer to a functional account or database account for the application. This is the way to go. If you need to log who did the request your application should have user authentication and do the logging of the request.
The other alternative of a database account per user is not scalable and if you have to provide a database id for each user, which the user will be using to connect, it also has security implications.
By having the application between the user and the database you isolate the database from the outside and the only access is what the applications permits.

can a non-dbo sql server user create a queue in service broker?

we have an application being developed where it is running as the AD user connecting to a service broker enabled SQL Server 2008 database.
We are having trouble granting the right permissions to get the app to work for the specific user,
(the app creates/deletes queues during its operation) unless we make that user a member of dbo for the database being used for the service broker application.
Is this the only way we can get the app to work, put each user as a member of dbo in the database?
Granting CREATE QUEUE/SERVICE TO user and SELECT on the specfic tables doesnt seem to be enough.
thanks
Due to the distributed nature of Service Broker, queues and services should be long lived objects. An application that creates and drops queues and services is very unlikely to act correctly.
That being said, the required permission is RECEIVE to operate on a queue (BEGIN CONVERSATION/SEND/RECEIVE). The user that created a queue is the queue owner and has this permission implicitly. To deliver messages to a target service the initiator service must have SEND permission, but be carefull that identity is established using Service Broker dialog security which is based on certificates. Note though that * within a SQL Server instance* the Service Broker conversation security model will emulate the familiar model of user-id (ie. no certificates are required to establish identity), but is subject to EXECUTE AS context.
If you can be more specific about which operation fails, we can be more specific about what permission is required.
Create separate schema per user and make the user the owner of that schema. Service broker will create the sprocs and queues in the default schema of the user. This post on how to setup service broker permissions may help you: http://kreelbits.blogspot.com/2014/10/microsoft-sql-useful-database-role-for.html

SQL Login notification

I want to be notified of SQL Server logins that has a particular value for the Application Name property of the connection string. Can someone help me to get these type of notifications by email or by any other recommended method?
You're looking for a logon trigger. In the trigger you can inspect the connection application name (note that this setting is not secure and can be easily spoofed). Use sp_send_dbmail to send yourself mail.

Resources