Entries Tagged as "SQL Server"

Adding cftransactions to prevent deadlocks in SQL Server

I recently had to dive into errors from one of our most busy web applications. The errors came from SQL Server, saying "Transaction (Process ID 51) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.".

This error comes from SQL Server, and more or less says "I could not execute your query, because it got in the way of another query". Veeery simply said, I know.

First thing I did, was asking the Railo team whether we could create a new option in the datasource screen "retry deadlocked query", with a number indicating the maximum number of retries. Initial reactions were positive, so I added a feature request into the Railo JIRA.

But, problem still remains for now. In my situation, it was always a SELECT query which was chosen as the deadlock victim. Now, I did a lot of googling and reading about preventing or circumventing this problem, and the most common answer was "use Read Uncommitted with your select statements, if you don't mind dirty reads". After discussing a bit at the office, we decided we don't mind, since we couldn't think of a really problematic scenario with it.

So, we had to change all select queries from...

5 Comments

Change SQL Server bit field to the opposite value: "NOT bit_fld"

I had a DB column named 'mai_confirmed' (confirmed for mailing), but I needed a column with the value for 'needsConfirmation' for a mailing component.

Or, to say it codish: needsConfirmation = not mai_confirmed

I tried SELECT !mai_confirmed AS needsConfirmation, and also SELECT (NOT mai_confirmed) AS needsConfirmation, but those didn't work.

The trick is:

SELECT mai_confirmed + 1 % 2 AS needsConfirmation

Even though the resulting value is not a bit field anymore, it does return either NULL, 1, or 0. Which was fine enough for me ;-)

9 Comments

Custom sql server 2005 backup script

I finally managed to fully automate the backups for my sql server 2005 express edition databases. The product is free, and therefor great. But the limitations seem to be the jobs you can't add, and the scheduling that isn't available.

So, I made this small but handy Stored procedure, which queries all database names, loops through them, and creates a backup file for each of them:

No Comments

thunderous