Windows 2012 Hosting - MVC 6 and SQL 2014 BLOG

Tutorial and Articles about Windows Hosting, SQL Hosting, MVC Hosting, and Silverlight Hosting

SQL 2012 Hosting with ASPHostPortal.com:: How to Handle Deadlock in SQL Server with Script

clock June 13, 2014 09:30 by author Ben

A deadlock is an inevitable situation in the RDBMS architecture and very common in high-volume OLTP environments. A deadlock occurs when two (or more) processes attempt to access a resource that the other process holds a lock on. Because each process has a request for another resource, neither process can be completed. When a deadlock is detected, SQL Server rolls back the command that has the least processing time and returns error message 1205 to the client application. This error is not fatal and may not cause the batch to be terminated.

Here is the simple script to handle deadlock monitoring using T-SQL code

CREATE EVENT SESSION [Deadlock_Monitor] ON SERVER
 ADD EVENT sqlserver.xml_deadlock_report
 ADD TARGET package0.event_file(SET filename=N'C:\Temp\Deadlock_Monitor.xel')
 WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,
 MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,
 TRACK_CAUSALITY=OFF,STARTUP_STATE=ON)
 GO

For another way, there is two method with script to handle deadlock in SQL Server, such as:

  • method 1

/*--
-- ,,
-- ,,
-- ,,,
-- 2004.4--
--
 exec p_lockinfo
--*/
create proc p_lockinfo
    @kill_lock_spid bit=1, --,1 , 0
    @show_spid_if_nolock bit=1 --,,1 ,0
as
    declare @count int,@s nvarchar(1000),@i int
    select id=identity(int,1,1),,
        ID=spid,ID=kpid,ID=blocked,ID=dbid,
        =db_name(dbid),ID=uid,=loginame,CPU=cpu,
        =login_time,=open_tran, =status,
        =hostname,=program_name,ID=hostprocess,
        =nt_domain,=net_address
    into #t from(
        select ='',
            spid,kpid,a.blocked,dbid,uid,loginame,cpu,login_time,open_tran,
            status,hostname,program_name,hostprocess,nt_domain,net_address,
            s1=a.spid,s2=0
        from master..sysprocesses a join (
        select blocked from master..sysprocesses group by blocked
        )b on a.spid=b.blocked where a.blocked=0
        union all
        select '|__>',
            spid,kpid,blocked,dbid,uid,loginame,cpu,login_time,open_tran,
            status,hostname,program_name,hostprocess,nt_domain,net_address,
            s1=blocked,s2=1
        from master..sysprocesses a where blocked<>0
        )a order by s1,s2

    select @[email protected]@rowcount,@i=1

    if @count=0 and @show_spid_if_nolock=1
    begin
        insert #t
        select ='',
            spid,kpid,blocked,dbid,db_name(dbid),uid,loginame,cpu,login_time,
            open_tran,status,hostname,program_name,hostprocess,nt_domain,net_address
        from master..sysprocesses
        set @[email protected]@rowcount
    end

    if @count>0
    begin
        create table #t1(id int identity(1,1),a nvarchar(30),b Int,EventInfo nvarchar(255))
        if @kill_lock_spid=1
            begin
                declare @spid varchar(10),@ varchar(10)
                while @i<[email protected]
                begin
                    select @spid=ID,@= from #t where [email protected]
                    insert #t1 exec('dbcc inputbuffer('[email protected]+')')
                    if @='' exec('kill '[email protected])
                    set @[email protected]+1
                end
            end
        else
            while @i<[email protected]
            begin
                select @s='dbcc inputbuffer('+cast(ID as varchar)+')' from #t where [email protected]
                insert #t1 exec(@s)
                set @[email protected]+1
            end
        select a.*,SQL=b.EventInfo
        from #t a join #t1 b on a.id=b.id
    end
go

  • method 2


SELECT
request_session_id as Spid,
Coalesce(s.name + '.' + o.name + isnull('.' + i.name,''),
s2.name + '.' + o2.name,
db.name) AS Object,
l.resource_type as Type,
request_mode as Mode,
request_status as Status
FROM sys.dm_tran_locks l
LEFT JOIN sys.partitions p
ON l.resource_associated_entity_id = p.hobt_id
LEFT JOIN sys.indexes i
ON p.object_id = i.object_id
AND p.index_id = i.index_id
LEFT JOIN sys.objects o
ON p.object_id = o.object_id
LEFT JOIN sys.schemas s
ON o.schema_id = s.schema_id
LEFT JOIN sys.objects o2
ON l.resource_associated_entity_id = o2.object_id
LEFT JOIN sys.schemas s2
ON o2.schema_id = s2.schema_id
LEFT JOIN sys.databases db
ON l.resource_database_id = db.database_id
WHERE resource_database_id = DB_ID()
ORDER BY Spid, Object, CASE l.resource_type
When 'database' Then 1
when 'object' then 2
when 'page' then 3
when 'key' then 4
Else 5 end


SQL Server 2012 Hosting is Here with FREE Trial!

Start your FREE Trial SQL 2012 Hosting hosting with us and get professional web hosting support! If the service does not meet your expectations simply cancel before the end of the free trial period. Risk FREE! Why wait longer?

You can start from as low as $5.00/month to start hosting your SQL 2012 on our environment. If you do not have a domain name, please do not worry as we will give you one FREE domain name (worth $14.99/year) if you register for any of our hosting plans for 12 months service(*). We will do our best to help you create your first web presence on the internet and we will continuously support the growth of your business.

 



FREE Trial SQL 2012 Hosting - with ASPHostPortal.com :: New Security Features SQL Server 2012

clock March 18, 2014 12:12 by author Diego

Microsoft SQL Server 2012 continues this trend with an extensive collection of new security features and enhancements. These enhancements not only help organizations to improve access controls to data, but also to achieve the highest level of data protection and compliance. Also, these features help make SQL Server arguably the most robust common database platform from a security perspective, with less vulnerability and fewer security patches needed to maintain the system.

SQL Server 2012 has many new security features, and three of the bigger new features are: Default Schema for Windows Groups, Audit enhancements, and User-Defined Server Roles.

Default schema for Windows groups
A database schema can now be tied to a Windows Group rather than an individual user in order to increase database compliance.  This makes it easier to administer database schemas, decreases the complexity of database schema management through individual Windows users, prevents errors of assigning a schema to the wrong user when a user changes groups, avoids unnecessary implicit schema creation, and by reducing the chances of choosing the wrong schema then it greatly reduces the chance of query errors.

Before SQL Server 2012 was introduced, it was not possible to specify the default schema for Windows groups. As a result, when the user getting access through Windows group membership created database objects such as a table or view inside a database, SQL Server automatically created a separate user (mapped to the admin account) and a schema with the same name in the database. Because of this security manageability issue, we end up having hundreds of users and schemas inside databases, which caused administrative challenges and is a managerial nightmare for administrators. Hence, SQL Server community requested a fix for this security issue via the Microsoft Connect site.

Luckily, SQL Server 2012 addresses this security issue by allowing us to assign a default schema for Windows Groups, which helps organizations simplify their database schema administration.

The following Transact SQL (T-SQL) demonstrates the process of assigning the default schema for Windows Group:

-- Creating Default Schema "ProdAdmins" for Windows Group "MyDomain\ProdDBAs"
CREATE SCHEMA [ProdAdmins] AUTHORIZATION [MyDomain\ProdDBAs]
GO


-- Set Default Schema for Windows Group "MyDomain\ProdDBAs"
ALTER USER [MyDomain\ProdDBAs] WITH DEFAULT_SCHEMA=[ProdAdmins]
GO

Audit Enhancements
Server and database audit specification objects found in SQL Server 2008 and SQL Server 2008 R2 are the most useful features of SQL Server and help the organization meet various regulatory compliance requirements. The problem with these auditing features is that they were only in the enterprise edition.

Fortunately, the server level audit specification features are now supported by all versions of SQL Server 2012. Audit specification features of SQL Server 2012 are more resilient to failures with writing to the audit log, and it is possible to limit the number of audit log files without rolling over. SQL Server 2012 audit specification features also support user-defined groups, which means we can now write audited events to the audit log by using sp_audit_write (Transact-SQL) procedure. Finally, SQL Server 2012 supports the ability to filter the audit events and include new audited groups to monitor contained database users.

User-Defined Server Roles
User-Defined Server Roles increase flexibility, manageability, and facilitates compliance towards better separation of duties.  It allows creation of new server roles to suit different organizations that separate multiple administrators according to roles.  Roles can also be nested to allow more flexibility in mapping to hierarchical structures in organizations.  It also helps prevent organizations from using sysadmin for database administration. We can use CREATE SERVER ROLE, ALTER SERVER ROLE and DROP SERVER ROLE Transact-SQL statements to create, alter and drop user-defined server roles. This is demonstrated as follows:

-- Creating user-defined roles
CREATE SERVER ROLE [JuniorDBA]


-- Granting server-wide permissions

GRANT CREATE ANY DATABASE TO [JuniorDBA]

-- Adding members to user-defined roles
ALTER SERVER ROLE [JuniorDBA]
ADD MEMBER [Domain\JuniorDBA_Group1]
ALTER SERVER ROLE [JuniorDBA]
ADD MEMBER [Domain\JuniorDBA_Group1]

-- Making user-defined role member of fixed server role ALTER SERVER ROLE [processadmin]
ADD MEMBER [JuniorDBA]


-- Dropping user-defined roles
DROP SERVER ROLE [JuniorDBA]



About ASPHostPortal.com

We’re a company that works differently to most. Value is what we output and help our customers achieve, not how much money we put in the bank. It’s not because we are altruistic. It’s based on an even simpler principle. "Do good things, and good things will come to you".

Success for us is something that is continually experienced, not something that is reached. For us it is all about the experience – more than the journey. Life is a continual experience. We see the Internet as being an incredible amplifier to the experience of life for all of us. It can help humanity come together to explode in knowledge exploration and discussion. It is continual enlightenment of new ideas, experiences, and passions


Author Link


Corporate Address (Location)

ASPHostPortal
170 W 56th Street, Suite 121
New York, NY 10019
United States

Sign in