Skip to content

Commit

Permalink
Allow ability to OpenSharedConnection lazily but not auto close
Browse files Browse the repository at this point in the history
  • Loading branch information
schotime committed Jan 20, 2024
1 parent 304766d commit 2f1a807
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/NPoco.Abstractions/IAsyncBaseDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public interface IAsyncBaseDatabase : IBaseDatabase, IAsyncDisposable
/// Opens the DbConnection manually
/// </summary>
Task<IAsyncDatabase> OpenSharedConnectionAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Opens the DbConnection manually
/// </summary>
Task<IAsyncDatabase> OpenSharedConnectionAsync(OpenConnectionOptions options = null, CancellationToken cancellationToken = default);

/// <summary>
/// Closes the DBConnection manually
Expand Down
2 changes: 1 addition & 1 deletion src/NPoco.Abstractions/IBaseDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public interface IBaseDatabase : IDisposable, IDatabaseConfig
/// <summary>
/// Opens the DbConnection manually
/// </summary>
IDatabase OpenSharedConnection();
IDatabase OpenSharedConnection(OpenConnectionOptions options = null);

/// <summary>
/// Closes the DBConnection manually
Expand Down
8 changes: 8 additions & 0 deletions src/NPoco.Abstractions/OpenConnectionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#nullable enable
namespace NPoco
{
public class OpenConnectionOptions
{
public bool Lazy { get; set; }
}
}
17 changes: 15 additions & 2 deletions src/NPoco/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ public void Dispose()

private bool ShouldCloseConnectionAutomatically { get; set; }

private OpenConnectionOptions OpenConnectionOptions { get; set; } = new();

// Open a connection (can be nested)
public IDatabase OpenSharedConnection()
public IDatabase OpenSharedConnection(OpenConnectionOptions? options = null)
{
OpenConnectionOptions = options ?? new();
OpenSharedConnectionImp(false, true).RunSync();
return this;
}
Expand All @@ -116,6 +119,13 @@ public async Task<IAsyncDatabase> OpenSharedConnectionAsync(CancellationToken ca
return this;
}

public async Task<IAsyncDatabase> OpenSharedConnectionAsync(OpenConnectionOptions? options = null, CancellationToken cancellationToken = default)
{
OpenConnectionOptions = options ?? new();
await OpenSharedConnectionImp(false, false, cancellationToken);
return this;
}

private void OpenSharedConnectionInternal()
{
OpenSharedConnectionImp(true, true).RunSync();
Expand All @@ -134,7 +144,10 @@ private async Task OpenSharedConnectionImp(bool isInternal, bool sync, Cancellat
if (_sharedConnection != null && _sharedConnection.State != ConnectionState.Broken && _sharedConnection.State != ConnectionState.Closed)
return;

ShouldCloseConnectionAutomatically = isInternal;
if (!isInternal && OpenConnectionOptions.Lazy)
return;

ShouldCloseConnectionAutomatically = isInternal && !OpenConnectionOptions.Lazy;

_sharedConnection = _factory?.CreateConnection()!;
if (_sharedConnection == null) throw new Exception("SQL Connection failed to configure.");
Expand Down
58 changes: 58 additions & 0 deletions test/NPoco.Tests/ConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,63 @@ public void ConstructorWithConnectionStringDBProviderAndSettings()
db.Dispose();
Assert.IsNull(db.Connection);
}

[Test]
public void OpenSharedConnectionLazy()
{
var dbType = GetConfiguredDatabaseType();
var db = new Database(TestDatabase.ConnectionString, dbType, SqlClientFactory.Instance);
db.OpenSharedConnection(new OpenConnectionOptions() { Lazy = true });
Assert.IsNull(db.Connection);

var forty2 = db.Single<int>("select 42");
Assert.AreEqual(42, forty2);
Assert.IsNotNull(db.Connection);
Assert.IsTrue(db.Connection.State == ConnectionState.Open);

db.CloseSharedConnection();
Assert.IsNull(db.Connection);

db.Dispose();
Assert.IsNull(db.Connection);
}

[Test]
public void OpenSharedConnectionNotLazy()
{
var dbType = GetConfiguredDatabaseType();
var db = new Database(TestDatabase.ConnectionString, dbType, SqlClientFactory.Instance);
db.OpenSharedConnection(new OpenConnectionOptions() { Lazy = false });
Assert.IsNotNull(db.Connection);

var forty2 = db.Single<int>("select 42");
Assert.AreEqual(42, forty2);
Assert.IsNotNull(db.Connection);
Assert.IsTrue(db.Connection.State == ConnectionState.Open);

db.CloseSharedConnection();
Assert.IsNull(db.Connection);

db.Dispose();
Assert.IsNull(db.Connection);
}

[Test]
public void OpenSharedConnectionImplicit()
{
var dbType = GetConfiguredDatabaseType();
var db = new Database(TestDatabase.ConnectionString, dbType, SqlClientFactory.Instance);
Assert.IsNull(db.Connection);

var forty2 = db.Single<int>("select 42");
Assert.AreEqual(42, forty2);
Assert.IsNull(db.Connection);

db.CloseSharedConnection();
Assert.IsNull(db.Connection);

db.Dispose();
Assert.IsNull(db.Connection);
}
}
}

0 comments on commit 2f1a807

Please sign in to comment.