ServiceStack OrmLite Command Timeout
Asked Answered
G

2

6

When using IDbConnection.ExecuteSql how do I set the Command Timeout?

IDbConnection db = ConnectionFactory.OpenDbConnection();
db.ExecuteSql("...");

If I use the IDbCommand.ExecuteSql ( See below ) method I can set the Command Timeout, but I get a bunch of warnings about deprecated methods.

IDbCommand comm = db.CreateCommand()
comm.CommandTimeout = 240;                    
comm.ExecuteSql("...");
Genia answered 12/3, 2013 at 5:6 Comment(0)
H
4

With the most recent change OrmLite no longer provides APIs around the IDbCommand object directly (which have now all been made internal in the latest version).

But since OrmLite is only extension methods over ADO.NET's underlying IDbConnection and IDbCommand interfaces, you can easily by-pass OrmLite's extension methods when you need to and just use them directly, e.g:

IDbConnection db = ConnectionFactory.OpenDbConnection();
IDbCommand cmd = db.CreateCommand();
cmd.CommandTimeout = 240;  
cmd.CommandText = "...";
cmd.ExecuteNonQuery();

Alternatively you can set a global CommandTimeout with:

OrmLiteConfig.CommandTimeout = 240;
Haupt answered 12/3, 2013 at 5:31 Comment(6)
What if we want to use sql generated by db.Select. When we want to set timeout, we should use custom handwritten sql queries ? @HauptWellmeaning
@mustafasturan, having same issue... I use IDbConnection.Query<> and I need to specify the timeout of that.Biliary
Is this still the best solution? It seems like if you set the CommandTimeout paramater on the connection string that all commands created using that dbconnection should use the supplied timeout. Is there a possiblity of this behavior being changed?Hellenhellene
@Hellenhellene I've updated answer to include OrmLiteConfig.CommandTimeout exampleHaupt
Are there any plans to allow it to be set, non globally? I was hoping to allow consumers of an api to set it per requestHellenhellene
@Hellenhellene this is only other alternative the only plans for new features are maintained on UserVoiceHaupt
B
4

A bit late to the party and as you've seen in my comment, I've had this exact issue. My solution was to extend on myth's suggestion and create a new extension method:

public static partial class IDbConnectionExtensionMethods
{
    public static List<T> Query<T>(this IDbConnection self, string sql, int commandTimeout)
    {
        List<T> results = null;
        self.Exec((dbCmd) =>
            {
                dbCmd.CommandTimeout = commandTimeout;
                dbCmd.CommandText = sql;
                using (var reader = dbCmd.ExecuteReader())
                {
                    results = reader.ConvertToList<T>();
                }
            });

        return results;
    }   // eo Query<T>
}   // eo class IDbConnectionExtensionMethods
Biliary answered 30/7, 2013 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.