Skip to content

Commit

Permalink
Add connection state change events for close and open. Thanks
Browse files Browse the repository at this point in the history
https://github.com/pdonald and https://github.com/maxbundchen for the
patch.

Npgsql didn't have connection state change events. According to
maxbundchen: "the EF6 depends on open/close events in some cases (like
update or insert) and Npgsql don't trigger this ones."
#19 (comment)

Added tests about those events.
  • Loading branch information
franciscojunior committed Jun 12, 2013
1 parent 17ad626 commit e6f6cf7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Npgsql/NpgsqlConnection.cs
Expand Up @@ -576,6 +576,9 @@ public override void Open()
{
Promotable.Enlist(Transaction.Current);
}

this.OnStateChange (new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open));

}

/// <summary>
Expand Down Expand Up @@ -669,6 +672,9 @@ private void ReallyClose()
}

connector = null;

this.OnStateChange (new StateChangeEventArgs(ConnectionState.Open, ConnectionState.Closed));

}

/// <summary>
Expand Down
31 changes: 31 additions & 0 deletions testsuite/noninteractive/NUnit20/ConnectionTests.cs
Expand Up @@ -371,6 +371,37 @@ public void GetSchemaForeignKeys()
Assert.IsNotNull(dt);


}

[Test]
public void ChangeState()
{

using (NpgsqlConnection c = new NpgsqlConnection (TheConnectionString))
{
bool stateChangeCalledForOpen = false;
bool stateChangeCalledForClose = false;


c.StateChange += new StateChangeEventHandler( delegate(object sender, StateChangeEventArgs e) {
if (e.OriginalState == ConnectionState.Closed && e.CurrentState == ConnectionState.Open)
stateChangeCalledForOpen = true;

if (e.OriginalState == ConnectionState.Open && e.CurrentState == ConnectionState.Closed)
stateChangeCalledForClose = true;
});

c.Open ();
c.Close ();

Assert.IsTrue (stateChangeCalledForOpen);
Assert.IsTrue (stateChangeCalledForClose);

}




}

}
Expand Down

0 comments on commit e6f6cf7

Please sign in to comment.