After updating tables in sql they rollback the following day - sql-server

Everytime when I update the table rows in SQL Server, the update runs successfully and I also confirm and it shows that the rows were updated. However, the following day when I check I find that the updates I made have rolled back. I also checked if there are any transactions opened but there is none. I do not run my update statement in a transaction, therefore I do not have to commit any transaction after.
This is been happening for a while and it is really irritating.
Any help will be highly appreciated.

It could be possible that your server or data access mechanism you're using (ie. ADO.NET) isn't configured to auto-commit transactions. This MSDN site states;
Autocommit mode is the default transaction management mode of the SQL
Server Database Engine. Every Transact-SQL statement is committed or
rolled back when it completes. If a statement completes successfully,
it is committed; if it encounters any error, it is rolled back. A
connection to an instance of the Database Engine operates in
autocommit mode whenever this default mode has not been overridden by
either explicit or implicit transactions. Autocommit mode is also the
default mode for ADO, OLE DB, ODBC, and DB-Library.
Try explicitly putting your UPDATE into a transaction and committing to see if that saves. Also ask your DBA to see if there is any setting on SQL Server that has change the default transaction management mode to set autocommit to false.

Related

SQL Server alter database with rollback immediate

Sometimes when (for example) setting a database offline by executing the following command, an exception will be thrown, because one or more clients is connected:
ALTER DATABASE <dbname> SET OFFLINE
According to this answer one solution is to use with rollback immediate at the end:
ALTER DATABASE <dbname> SET OFFLINE WITH ROLLBACK IMMEDIATE
But why is actually this disconnecting other clients? It sounds like a very non-obvious way to force disconnections, although it works.
You can't take the database offline while other Users have transactions running on it so you're going to have to disconnect the users one way or another to take the database offline.
If you were being polite you could ask all your users to finish doing whatever they were doing before you took the database offline
but if that's not practical including the option
WITH ROLLBACK IMMEDIATE
Means that all current transactions are Rolled back (the database is reset to the point where the transaction started) . You could also use
WITH ROLLBACK AFTER 60 SECONDS
but I've never had to
If you could take a database offline while there were unfinished transactions were running then the database would be in an unstable state with the change neither made nor cancelled
From MSDN
WITH <termination>::=
Specifies when to roll back incomplete transactions when the database
is transitioned from one state to another. If the termination clause
is omitted, the ALTER DATABASE statement waits indefinitely if there
is any lock on the database. Only one termination clause can be
specified, and it follows the SET clauses.
Where termination would be ROLLBACK IMMEDIATE in your case.

What could be the reason for SQL Server Express to roll back a database to a previous state?

I have some very strange problems. I have an application running on Windows 2003 terminal server from multiple clients. The application uses SQL Server 2008 Express as its database.
Yesterday, I connected to the app, closed some sessions on the server that were not responding, and to my surprise, I saw that some data was missing from the database. After a futher search I found that all the database changes made from last week were lost.
It's like the database rolled back all the changes, and returned to the state of one week ago! I can confirm that all the changes were lost. In fact I have inserted a record into a table with identity_insert ON (to manually insert an ID on an autonumeric col) and that record is missing, so there is no way this is a program failure.
Does anyone have any idea of what could have happend here?
EDIT
I have a suspect: could a transaction initiated by a session stays in a unconfirmed state for one week, retain all the database changes and when I close the session rollback all the changes made?
EDIT II
Find this on log:
SQL Server never rolls back a database to a previous state (like this). The database was restored, or the entire disk/VM was rolled back, or DML was executed to create the impression that a rollback happened (but really didn't). Maybe someone executed a sync tool in the wrong direction.
The question does not have information that allows for finding the problem. But it certainly isn't SQL Server rolling back a database.
You can try examining the log using fn_dblog.
From the log it looks like the server has only just started up after a reboot or service restart.
If a database is not cleanly shut down then the database can be left with partially applied transactions. If this happens then the database is recovered on start up.
Any transactions that are incomplete are rolled back. Committed transactions that were not yet applied are rolled forwards. How long this recovery takes depends on the size of the transactions in the log that have not yet been applied to the database.
The transactions may not show up in the log after they have been rolled back following a crash. This depends upon their location in the log and the databases's recovery mode.
If the transaction is at the end of the log it is likely the log will just be rolled back and the transaction removed.
If the transaction is in the middle of the log you might see a LOP_ABORT_XACT in the log.
When using simple recovery there is a good chance the log will be cleared after recovery (since the logs are only kept until the transactions are committed).
See Are log records removed from ldf file for rollbacks? for more details.

Locking single row / row level when inserting data via TransactionScope, Entity Framework and SQL Server

I did some research and haven't found any explanation to this. So, If there's one out there, I'm sorry.
I am using TransactionScope to handle transactions with my SQL Server 2012 database. I'm also using Entity Framework.
The fact is that when I start a new transaction to insert a new record into my table, it locks the entire table, not just that row. So, if I run the Db.SaveChanges(), without committing it, and go to management studio and try to get the the already committed data from the same table, it hangs and return me no data.
What I would like in this scenario is to lock just the new row, not the entire table.
Is that possible?
Thank you in advance.
One thing to be very careful of when using TransactionScope is that it uses Serializable isolation level by default which can cause many locking issues in SQL Server. The default isolation level in SQL Server is Read Committed, so you should consider using that in any transactions that use TransactionScope. You can factor out a method that creates your default TransactionScope and always set to ReadCommitted by default (see Why is System.Transactions TransactionScope default Isolationlevel Serializable). Also ensure that you have a using block when using TransactionScope, to make sure that if errors occur with the transaction processing that the transaction is rolled back (http://msdn.microsoft.com/en-us/library/yh598w02.aspx).
By default, SQL Server uses a pessimistic concurrency model, which means that as DML commands are being processed (inserts, updates, deletes), it will acquire an exclusive lock on the data that is changing, which will prevent other updates or SELECTs from completing until those locks are released. The only way to release those locks is to commit or rollback the transaction. So if you have a transaction that is inserting data into a table, and you run a SELECT * FROM myTable before the insert has completed, then SQL Server will force your select to wait until the open transaction has been commit or rolled back before returning the results. Normally transactions should be small and fast, and you would not notice as much of an issue. Here is more info on isolation levels and locking (http://technet.microsoft.com/en-us/library/ms378149.aspx).
In your case, it sounds like you are debugging, and have hit a breakpoint in the code with the transaction open. For debugging purposes, you can add a nolock hint to your query, which would show the results of the data that has been committed, along with the insert which has not yet been committed. Because using nolock will return UN-committed data, be very careful about using this in any production environment. Here is an example of a query with a nolock hint.
SELECT * FROM myTable WITH(NOLOCK)
If you continue to run into locking issues outside of debugging, then you can also check out snapshot isolation (Great article by Kendra Little: http://www.brentozar.com/archive/2013/01/implementing-snapshot-or-read-committed-snapshot-isolation-in-sql-server-a-guide/). There are some special considerations when using snapshot isolation, such as tempdb tuning.

Database Connection and Rollback Process

I wonder a case. I have a project using a database (Oracle and Mssql). My project has a framework that I manage transactions.
In thread I open a database connection and start a transaction.(In transaction, there are many update and insert queries.) While code is running, somehow connection is closed. Because I have try-catch block, I catch exception and rollback transaction. BUT; if my connection is closed because some reasons, how rollback query can run on database? How can I handle this situation? If I open a new connection and rollback, does it work?
Thanks.
There is a term you should know - ACID compliancy:
Atomicity is an all-or-none proposition;
Consistency guarantees that a transaction never leaves your database in a half-finished state.
Isolation keeps transactions separated from each other until they’re finished.
Durability guarantees that the database will keep track of pending changes in such a way that the server can recover from an abnormal termination.
Concerning MySQL
In order to get this at MySQL, you have to use Transaction Safe Tables (TST). Advantages of Transaction-Safe Tables:
Safer. Even if MySQL crashes or you get hardware problems, you can get your data back, either by automatic recovery or from a backup + the transaction log.
You can combine many statements and accept these all in one go with the COMMIT command.
You can execute ROLLBACK to ignore your changes (if you are not running in auto-commit mode).
If an update fails, all your changes will be restored.
Concerning SQL Server
You should read "Transaction Behavior On Lost Connection" MSDN forum topic.
To understand better what lays behind MS SQL Server transactions, read a good article "Locks and Duration of Transactions in MS SQL Server"
Make sure you are not using any autocommit feature (I think that's enabled by default in some MySQL installations). If you do all your commits "manually", a broken connection will just result in you never committing the transaction and so it won't ever get written.
You cannot reconnect to rollback in most database systems.

How to rollback SQL update command?

In SQL server 2008 R2. Is it possible to do a rollback on a single update command?
I know there are other questiones like this on SO but i havent seen one specific for 2008 R2 and hence I may get the same answer, if that is the case then we can close this thread.
I did the a simple update without any transactions commands:
UPDATE myTable SET col1=somevalue WHERE....
Of course you can use explicit transactions such as
BEGIN TRAN
UPDATE ...
ROLLBACK
but I don't think you are asking about that?
If you have the option SET IMPLICIT_TRANSACTIONS ON then the command will not be committed or rolled back until you do so explicitly but this is not the default behaviour.
By default transactions are auto committed so when the command finishes successfully the results of the update will be committed. If the update was to encounter an error - including the connection being killed mid update it would auto rollback.
If your database is in full recovery mode you might want to try reading transaction log, finding which rows were affected and then reverting the update.
However, this is not supported by default, because MS stored transaction log in its own format that is not well documented.
Solution is to use commands such as DBCC LOG or fn_log or third party tool such as ApexSQL Log which does all of this automatically but comes with a price.
If you need more details, here are couple of posts on reading transaction log:
Read the log file (*.LDF) in sql server 2008
SQL Server Transaction Log Explorer/Analyzer

Resources