Skip to content

Commit

Permalink
Reduced memory footprint. Closes #65.
Browse files Browse the repository at this point in the history
Command and reader objects are immediately disposed in order to reduce
the memory footprint. This is particular relevant on mobile devices
with small amounts of available memory.
  • Loading branch information
hisystems committed Aug 2, 2012
1 parent 72f9a0c commit 2f28f28
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 76 deletions.
32 changes: 26 additions & 6 deletions Database/ConnectionScope.cs
Expand Up @@ -25,15 +25,24 @@ public class ConnectionScope : IDisposable
/// If a connection is already opened then the already open connection is utilised.
/// </summary>
public ConnectionScope(Database database)
: this(GetConnection(database))
{
if (database == null)
throw new ArgumentNullException();

connection = database.Connection;

connection.Start();
}

/// <summary>
/// Ensures that a new connection is opened.
/// If a connection is already opened then the already open connection is utilised.
/// </summary>
internal ConnectionScope(Database.ConnectionController connection)
{
if (connection == null)
throw new ArgumentNullException();

this.connection = connection;

connection.Start();
}

/// --------------------------------------------------------------------------------
/// <summary>
/// Executes the SQL statement.
Expand Down Expand Up @@ -104,5 +113,16 @@ public void Dispose()

GC.SuppressFinalize(this);
}

/// <summary>
/// Called by constructor.
/// </summary>
private static Database.ConnectionController GetConnection(Database database)
{
if (database == null)
throw new ArgumentNullException();

return database.Connection;
}
}
}
131 changes: 61 additions & 70 deletions Database/Database.cs
Expand Up @@ -1085,17 +1085,13 @@ public SQL.SQLFieldValues ObjectLockRecord(IDatabaseObjects objCollection, IData
if (objSubset != null && !objSubset.IsEmpty)
objSelect.Where.Add(objSubset);

IDataReader objReader = this.Transactions.Execute(objSelect);
SQL.SQLFieldValues objFieldValues;

if (objReader.Read())
objFieldValues = FieldValuesFromDataReader(objCollection, objReader);
else
throw new Exceptions.ObjectDoesNotExistException(objCollection, objItem.DistinctValue);

objReader.Close();

return objFieldValues;
using (var objReader = this.Transactions.Execute(objSelect))
{
if (objReader.Read())
return FieldValuesFromDataReader(objCollection, objReader);
else
throw new Exceptions.ObjectDoesNotExistException(objCollection, objItem.DistinctValue);
}
}

/// --------------------------------------------------------------------------------
Expand Down Expand Up @@ -1916,13 +1912,14 @@ internal bool InTransactionMode
/// </summary>
/// --------------------------------------------------------------------------------
public object ExecuteScalar(SQL.ISQLStatement objSQLStatement)
{
IDataReader objDataReader = ExecuteInternal(pobjConnection, objSQLStatement);

if (objDataReader.Read())
return objDataReader[0];
else
return null;
{
using (var objDataReader = ExecuteInternal(pobjConnection, objSQLStatement))
{
if (objDataReader.Read())
return objDataReader[0];
else
return null;
}
}

/// --------------------------------------------------------------------------------
Expand All @@ -1935,20 +1932,17 @@ public object ExecuteScalar(SQL.ISQLStatement objSQLStatement)
/// </summary>
/// --------------------------------------------------------------------------------
public object ExecuteScalarWithConnect(SQL.ISQLStatement objSQLStatement)
{
object result;

this.Start();

IDataReader objDataReader = ExecuteInternal(pobjConnection, objSQLStatement);
if (objDataReader.Read())
result = objDataReader[0];
else
result = null;

this.Finished();

return result;
{
using (var connectionScope = new ConnectionScope(this))
{
using (var objDataReader = connectionScope.Execute(objSQLStatement))
{
if (objDataReader.Read())
return objDataReader[0];
else
return null;
}
}
}

/// --------------------------------------------------------------------------------
Expand Down Expand Up @@ -2004,11 +1998,8 @@ public int ExecuteNonQuery(SQL.ISQLStatement[] objSQLStatements)
[Obsolete("Use DatabaseObjects.ConnectionScope")]
public int ExecuteNonQueryWithConnect(SQL.ISQLStatement objSQLStatement)
{
this.Start();
int result = this.ExecuteNonQuery(objSQLStatement);
this.Finished();

return result;
using (var connectionScope = new ConnectionScope(this))
return connectionScope.ExecuteNonQuery(objSQLStatement);
}

/// --------------------------------------------------------------------------------
Expand All @@ -2020,11 +2011,8 @@ public int ExecuteNonQueryWithConnect(SQL.ISQLStatement objSQLStatement)
[Obsolete("Use DatabaseObjects.ConnectionScope")]
public int ExecuteNonQueryWithConnect(SQL.ISQLStatement[] objSQLStatements)
{
this.Start();
int result = this.ExecuteNonQuery(new SQL.SQLStatements(objSQLStatements));
this.Finished();

return result;
using (var connectionScope = new ConnectionScope(this))
return connectionScope.ExecuteNonQuery(new SQL.SQLStatements(objSQLStatements));
}

protected virtual IDataReader ExecuteInternal(IDbConnection objConnection, SQL.ISQLStatement objSQLStatement)
Expand All @@ -2034,22 +2022,24 @@ protected virtual IDataReader ExecuteInternal(IDbConnection objConnection, SQL.I

objSQLStatement.ConnectionType = peConnectionType;
string strSQL = objSQLStatement.SQL;

var command = objConnection.CreateCommand();
command.CommandText = strSQL;

if (pobjTransactions.Count > 0)
command.Transaction = pobjTransactions.Peek(); //Only used for SQLServerCompactEdition
IDataReader reader;

using (var command = objConnection.CreateCommand())
{
command.CommandText = strSQL;

IDataReader reader;
if (pobjTransactions.Count > 0)
command.Transaction = pobjTransactions.Peek(); //Only used for SQLServerCompactEdition

try
{
reader = command.ExecuteReader();
}
catch (Exception ex)
{
throw new Exceptions.DatabaseObjectsException("Execute failed: " + strSQL, ex);
try
{
reader = command.ExecuteReader();
}
catch (Exception ex)
{
throw new Exceptions.DatabaseObjectsException("Execute failed: " + strSQL, ex);
}
}

OnStatementExecuted(objSQLStatement);
Expand All @@ -2061,27 +2051,28 @@ protected virtual int ExecuteNonQueryInternal(IDbConnection objConnection, SQL.I
{
if (objConnection == null)
throw new Exceptions.DatabaseObjectsException("Connection is not open, call Database.Connection.Start() or Database.Transactions.Begin()");

objSQLStatement.ConnectionType = peConnectionType;
string strSQL = objSQLStatement.SQL;
int rowAffected;

var command = objConnection.CreateCommand();
command.CommandText = strSQL;

if (pobjTransactions.Count > 0)
command.Transaction = pobjTransactions.Peek(); //Only used for SQLServerCompactEdition
using (var command = objConnection.CreateCommand())
{
command.CommandText = strSQL;

int rowAffected;
if (pobjTransactions.Count > 0)
command.Transaction = pobjTransactions.Peek(); //Only used for SQLServerCompactEdition

try
{
rowAffected = command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exceptions.DatabaseObjectsException("ExecuteNonQuery failed: " + strSQL, ex);
try
{
rowAffected = command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exceptions.DatabaseObjectsException("ExecuteNonQuery failed: " + strSQL, ex);
}
}

OnStatementExecuted(objSQLStatement);

return rowAffected;
Expand Down

0 comments on commit 2f28f28

Please sign in to comment.