Using code like
using (var tran = Ctxt.Database.BeginTransaction()) {
How can I set a value for the transaction timeout ?
Using code like
using (var tran = Ctxt.Database.BeginTransaction()) {
How can I set a value for the transaction timeout ?
If for whatever reason you need to manage transactions yourself it is much easier to use TransactionScope. It has several constructors accepting a TimeSpan
parameter to set the timeout. For instance
using(var ts = new TransactionScope(TransactionScopeOption.Required,
TimeSpan.FromMinutes(1)))
{
using(var ctx = new MyContext())
{
// Do stuff.
}
ts.Complete(); // Try - catch to catch TimeoutException
}
I'm curious though why you want to set transaction timeout, not command timeout.
My suggestion would be to use Database.CommandTimeout
:
var timeout = 60; //or whatever value you need
Ctxt.Database.CommandTimeout = timeout;
using (var tran = Ctxt.Database.BeginTransaction())
{
//do stuff
}
//this line can be skipped if you're about to dispose context
Ctxt.Database.CommandTimeout = null; //setting back default timeout
Of course, you can nicely wrap it in some class.