mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
UpgradeService now disposes NHibernate sessions, preventing possible SQL server connection pool saturation
This commit is contained in:
@@ -39,75 +39,74 @@ namespace Upgrade.Services {
|
||||
values.Add(parameters);
|
||||
});
|
||||
|
||||
var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
|
||||
var session = sessionFactory.OpenSession();
|
||||
|
||||
var connection = session.Connection;
|
||||
using (var session = _sessionFactoryHolder.GetSessionFactory().OpenSession()) {
|
||||
var connection = session.Connection;
|
||||
|
||||
foreach (var record in values) {
|
||||
var command = connection.CreateCommand();
|
||||
var statement = String.Format("INSERT INTO {0} (", toPrefixedTableName);
|
||||
foreach (var record in values) {
|
||||
var command = connection.CreateCommand();
|
||||
var statement = String.Format("INSERT INTO {0} (", toPrefixedTableName);
|
||||
|
||||
foreach (var keyValuePair in record) {
|
||||
if (ignoreColumns.Contains(keyValuePair.Key)) {
|
||||
continue;
|
||||
foreach (var keyValuePair in record) {
|
||||
if (ignoreColumns.Contains(keyValuePair.Key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
statement += keyValuePair.Key;
|
||||
if (keyValuePair.Key != record.Last().Key) {
|
||||
statement += ", ";
|
||||
}
|
||||
}
|
||||
|
||||
statement += keyValuePair.Key;
|
||||
if (keyValuePair.Key != record.Last().Key) {
|
||||
statement += ", ";
|
||||
}
|
||||
}
|
||||
statement += ") VALUES ( ";
|
||||
|
||||
statement += ") VALUES ( ";
|
||||
foreach (var keyValuePair in record) {
|
||||
if (ignoreColumns.Contains(keyValuePair.Key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var keyValuePair in record) {
|
||||
if (ignoreColumns.Contains(keyValuePair.Key)) {
|
||||
continue;
|
||||
var parameter = command.CreateParameter();
|
||||
parameter.ParameterName = "@" + keyValuePair.Key;
|
||||
parameter.Value = keyValuePair.Value;
|
||||
|
||||
statement += parameter.ParameterName;
|
||||
if (keyValuePair.Key != record.Last().Key) {
|
||||
statement += ", ";
|
||||
}
|
||||
|
||||
command.Parameters.Add(parameter);
|
||||
}
|
||||
|
||||
var parameter = command.CreateParameter();
|
||||
parameter.ParameterName = "@" + keyValuePair.Key;
|
||||
parameter.Value = keyValuePair.Value;
|
||||
statement += ")";
|
||||
|
||||
statement += parameter.ParameterName;
|
||||
if (keyValuePair.Key != record.Last().Key) {
|
||||
statement += ", ";
|
||||
}
|
||||
command.CommandText = statement;
|
||||
|
||||
command.Parameters.Add(parameter);
|
||||
}
|
||||
|
||||
statement += ")";
|
||||
|
||||
command.CommandText = statement;
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ExecuteReader(string sqlStatement, Action<IDataReader, IDbConnection> action) {
|
||||
var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
|
||||
var session = sessionFactory.OpenSession();
|
||||
using (var session = _sessionFactoryHolder.GetSessionFactory().OpenSession()) {
|
||||
var command = session.Connection.CreateCommand();
|
||||
command.CommandText = string.Format(sqlStatement);
|
||||
|
||||
var command = session.Connection.CreateCommand();
|
||||
command.CommandText = string.Format(sqlStatement);
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
while (reader != null && reader.Read()) {
|
||||
try {
|
||||
if (action != null) {
|
||||
action(reader, session.Connection);
|
||||
while (reader != null && reader.Read()) {
|
||||
try {
|
||||
if (action != null) {
|
||||
action(reader, session.Connection);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
Logger.Error(e, "Error while executing custom SQL Statement in Upgrade.");
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
Logger.Error(e, "Error while executing custom SQL Statement in Upgrade.");
|
||||
}
|
||||
}
|
||||
|
||||
if (reader != null && !reader.IsClosed) {
|
||||
reader.Close();
|
||||
if (reader != null && !reader.IsClosed) {
|
||||
reader.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,16 +121,15 @@ namespace Upgrade.Services {
|
||||
public bool TableExists(string tableName) {
|
||||
// While not particularly nice (or fast with many tables) this seems to be a database-agnostic way of checking the existence
|
||||
// of a table.
|
||||
var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
|
||||
var session = sessionFactory.OpenSession();
|
||||
using (var session = _sessionFactoryHolder.GetSessionFactory().OpenSession()) {
|
||||
var connection = session.Connection as DbConnection;
|
||||
|
||||
var connection = session.Connection as DbConnection;
|
||||
if (connection == null) {
|
||||
throw new InvalidOperationException("The database connection object should derive from DbConnection to check if a table exists.");
|
||||
}
|
||||
|
||||
if (connection == null) {
|
||||
throw new InvalidOperationException("The database connection object should derive from DbConnection to check if a table exists.");
|
||||
return connection.GetSchema("Tables").Rows.Cast<DataRow>().Any(row => row["TABLE_NAME"].ToString() == tableName);
|
||||
}
|
||||
|
||||
return connection.GetSchema("Tables").Rows.Cast<DataRow>().Any(row => row["TABLE_NAME"].ToString() == tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user