Permalink
Browse files

connection resources closed

- connection resources are closed by the end of request automatically
  • Loading branch information...
jakubmisek committed Jan 21, 2018
1 parent cfab164 commit 20f8529a4495a6c517bae2fa0942a2fdd165680d
@@ -65,7 +65,7 @@ internal sealed class PhpSqlDbConnection : ConnectionResource
/// <param name="manager">Containing connection manager.</param>
/// <param name="connectionString">Connection string.</param>
public PhpSqlDbConnection(SqlConnectionManager manager, string/*!*/ connectionString)
: base(connectionString, "mssql connection")
: base(manager.Context, connectionString, "mssql connection")
{
//if (ctx == null)
// throw new ArgumentNullException(nameof(ctx));
@@ -20,7 +20,8 @@ sealed class MySqlConnectionResource : ConnectionResource
readonly MySqlConnectionManager _manager;
readonly MySqlConnection _connection;

public MySqlConnectionResource(MySqlConnectionManager manager, string connectionString) : base(connectionString, "mysql connection")
public MySqlConnectionResource(MySqlConnectionManager manager, string connectionString)
: base(manager.Context, connectionString, "mysql connection")
{
_manager = manager;
_connection = new MySqlConnection(this.ConnectionString);
@@ -11,9 +11,14 @@ namespace Pchp.Library.Database
/// <summary>
/// Abstract base class for database connection managers.
/// </summary>
[PhpHidden]
public abstract class ConnectionManager<TConnection> where TConnection : ConnectionResource
public abstract class ConnectionManager<TConnection> : IStaticInit where TConnection : ConnectionResource
{
/// <summary>
/// Associated runtime context.
/// </summary>
public Context Context => _ctx;
Context _ctx;

/// <summary>
/// List of connections established by the manager.
/// </summary>
@@ -64,8 +69,9 @@ public TConnection CreateConnection(string connectionString, bool newConnection,
if ((success = connection.Connect()) == true)
{
_connections.Add(connection);
_ctx.RegisterDisposable(connection);
}

return connection;
}

@@ -84,24 +90,16 @@ TConnection GetConnectionByString(string connectionString)
return null;
}

/// <summary>
/// Removes last used connection from the list of active Connections.
/// </summary>
public void RemoveConnection()
{
if (_connections.Count > 0)
{
_connections.RemoveAt(_connections.Count - 1);
//Interlocked.Decrement(ref AppConnectionCount);
}
}

/// <summary>
/// Removes specified connection from the list of active connections.
/// </summary>
/// <param name="connection">The connection to be removed.</param>
public bool RemoveConnection(TConnection connection)
{
//
_ctx.UnregisterDisposable(connection);

//
if (_connections.Count != 0)
{
if (_connections.Remove(connection))
@@ -120,5 +118,10 @@ public bool RemoveConnection(TConnection connection)
/// </summary>
public TConnection GetLastConnection()
=> _connections.Count != 0 ? _connections[_connections.Count - 1] : null;

void IStaticInit.Init(Context ctx)
{
_ctx = ctx;
}
}
}
@@ -13,7 +13,6 @@ namespace Pchp.Library.Database
/// <summary>
/// Abstract class implementing common functionality of PHP connection resources.
/// </summary>
[PhpHidden]
public abstract class ConnectionResource : PhpResource
{
/// <summary>
@@ -62,9 +61,10 @@ public int LastAffectedRows
/// <summary>
/// Constructs the connection resource.
/// </summary>
/// <param name="ctx">Runtime context.</param>
/// <param name="connectionString"></param>
/// <param name="resourceTypeName"></param>
protected ConnectionResource(string connectionString, string resourceTypeName)
protected ConnectionResource(Context ctx, string connectionString, string resourceTypeName)
: base(resourceTypeName)
{
Debug.Assert(connectionString != null);
@@ -476,6 +476,44 @@ void ShutdownSessionHandler()

#endregion

#region Resources // objects that need dispose

HashSet<IDisposable> _lazyDisposables = null;

public void RegisterDisposable(IDisposable obj)
{
if (_lazyDisposables == null)
{
_lazyDisposables = new HashSet<IDisposable>();
}

_lazyDisposables.Add(obj);
}

public void UnregisterDisposable(IDisposable obj)
{
if (_lazyDisposables != null)
{
_lazyDisposables.Remove(obj);
}
}

void ProcessDisposables()
{
var set = _lazyDisposables;
if (set != null && set.Count != 0)
{
_lazyDisposables = null;

foreach (var x in set)
{
x.Dispose();
}
}
}

#endregion

#region Temporary Per-Request Files

/// <summary>
@@ -582,6 +620,7 @@ public virtual void Dispose()
try
{
ProcessShutdownCallbacks();
ProcessDisposables();
ShutdownSessionHandler();
//this.GuardedCall<object, object>(this.FinalizePhpObjects, null, false);
FinalizeBufferedOutput();

0 comments on commit 20f8529

Please sign in to comment.