pull/1/head
a.bozhenov 5 years ago
parent f43e2dca93
commit a064b5972d

@ -62,15 +62,22 @@ namespace ZeroLevel.SqlServer
} }
dbConnectionString = new SqlConnectionStringBuilder(_currentConnectionString); dbConnectionString = new SqlConnectionStringBuilder(_currentConnectionString);
dbConnectionString.Pooling = true; dbConnectionString.Pooling = true;
dbConnectionString.MaxPoolSize = 50;
} }
public SqlConnection CreateConnection() public SqlConnection CreateConnection()
{
try
{ {
var connection = new SqlConnection(ConnectionString); var connection = new SqlConnection(ConnectionString);
connection.Open(); connection.Open();
return connection; return connection;
} }
catch (Exception ex)
{
Log.Error(ex, $"[SqlDbConnectionFactory.CreateConnection] {ConnectionString}");
throw;
}
}
#region Helpers #region Helpers

@ -76,6 +76,7 @@ namespace ZeroLevel.SqlServer
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
} }
connection.Close();
} }
} }
@ -87,12 +88,19 @@ namespace ZeroLevel.SqlServer
public int ExecuteNonResult(string query, DbParameter[] par) public int ExecuteNonResult(string query, DbParameter[] par)
{ {
using (DbConnection connection = _factory.CreateConnection()) using (DbConnection connection = _factory.CreateConnection())
{
try
{ {
using (var cmd = CreateCommand(connection, query, par, Timeout)) using (var cmd = CreateCommand(connection, query, par, Timeout))
{ {
return cmd.ExecuteNonQuery(); return cmd.ExecuteNonQuery();
} }
} }
finally
{
connection.Close();
}
}
} }
public T Insert<T>(string insert_query, SqlParameter[] par) public T Insert<T>(string insert_query, SqlParameter[] par)
@ -108,6 +116,7 @@ namespace ZeroLevel.SqlServer
} }
finally finally
{ {
connection.Close();
connection.Dispose(); connection.Dispose();
} }
} }
@ -148,6 +157,7 @@ namespace ZeroLevel.SqlServer
da.Fill(ds); da.Fill(ds);
} }
} }
connection.Close();
} }
return ds; return ds;
} }
@ -164,6 +174,7 @@ namespace ZeroLevel.SqlServer
da.Fill(ds); da.Fill(ds);
} }
} }
connection.Close();
} }
return ds; return ds;
} }
@ -173,23 +184,37 @@ namespace ZeroLevel.SqlServer
public object ExecuteScalar(string query) public object ExecuteScalar(string query)
{ {
using (var connection = _factory.CreateConnection()) using (var connection = _factory.CreateConnection())
{
try
{ {
using (var cmd = CreateCommand(connection, query, null, Timeout)) using (var cmd = CreateCommand(connection, query, null, Timeout))
{ {
return cmd.ExecuteScalar(); return cmd.ExecuteScalar();
} }
} }
finally
{
connection.Close();
}
}
} }
public object ExecuteScalar(string query, DbParameter[] par) public object ExecuteScalar(string query, DbParameter[] par)
{ {
using (var connection = _factory.CreateConnection()) using (var connection = _factory.CreateConnection())
{
try
{ {
using (var cmd = CreateCommand(connection, query, par, Timeout)) using (var cmd = CreateCommand(connection, query, par, Timeout))
{ {
return cmd.ExecuteScalar(); return cmd.ExecuteScalar();
} }
} }
finally
{
connection.Close();
}
}
} }
#endregion #endregion
@ -197,6 +222,8 @@ namespace ZeroLevel.SqlServer
public int ExecProcedure(string name) public int ExecProcedure(string name)
{ {
using (var connection = _factory.CreateConnection()) using (var connection = _factory.CreateConnection())
{
try
{ {
using (var command = new SqlCommand(name, connection) using (var command = new SqlCommand(name, connection)
{ {
@ -207,6 +234,11 @@ namespace ZeroLevel.SqlServer
return command.ExecuteNonQuery(); return command.ExecuteNonQuery();
} }
} }
finally
{
connection.Close();
}
}
} }
#endregion #endregion
@ -281,7 +313,7 @@ namespace ZeroLevel.SqlServer
return new SqlParameter[0]; return new SqlParameter[0];
} }
private static SqlCommand CreateCommand(DbConnection connection, string query, DbParameter[] parameters, int timeout) public static SqlCommand CreateCommand(DbConnection connection, string query, DbParameter[] parameters, int timeout)
{ {
var command = connection.CreateCommand(); var command = connection.CreateCommand();
command.CommandText = query; command.CommandText = query;

Loading…
Cancel
Save

Powered by TurnKey Linux.