Skip to content

Commit

Permalink
Reapplied ConnectionStringBuilder caching update
Browse files Browse the repository at this point in the history
  • Loading branch information
kppullin authored and franciscojunior committed Jun 25, 2013
1 parent c8fe8e5 commit ff0a4b9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
71 changes: 54 additions & 17 deletions src/Npgsql/NpgsqlConnection.cs
Expand Up @@ -134,6 +134,9 @@ public sealed class NpgsqlConnection : DbConnection, ICloneable

private NpgsqlPromotableSinglePhaseNotification promotable = null;

// A cached copy of the result of `settings.ConnectionString`
private string _connectionString;


/// <summary>
/// Initializes a new instance of the
Expand All @@ -154,18 +157,8 @@ public NpgsqlConnection(String ConnectionString)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME, "NpgsqlConnection()");

NpgsqlConnectionStringBuilder builder = cache[ConnectionString];
if (builder == null)
{
settings = new NpgsqlConnectionStringBuilder(ConnectionString);
}
else
{
settings = builder.Clone();
}

LogConnectionString();

LoadConnectionStringBuilder(ConnectionString);

NoticeDelegate = new NoticeEventHandler(OnNotice);
NotificationDelegate = new NotificationEventHandler(OnNotification);

Expand Down Expand Up @@ -246,9 +239,14 @@ public NpgsqlConnection(String ConnectionString)
[Editor(typeof(ConnectionStringEditor), typeof(System.Drawing.Design.UITypeEditor))]
#endif

public override String ConnectionString
{
get { return settings.ConnectionString; }
public override String ConnectionString
{
get
{
if (string.IsNullOrEmpty(_connectionString))
RefreshConnectionString();
return settings.ConnectionString;
}
set
{
// Connection string is used as the key to the connector. Because of this,
Expand All @@ -264,7 +262,7 @@ public override String ConnectionString
{
settings = builder.Clone();
}
LogConnectionString();
LoadConnectionStringBuilder(value);
}
}

Expand Down Expand Up @@ -606,7 +604,10 @@ public override void ChangeDatabase(String dbName)

Close();

settings[Keywords.Database] = dbName;
// Mutating the current `settings` object would invalidate the cached instance, so work on a copy instead.
settings = settings.Clone();
settings[Keywords.Database] = dbName;
_connectionString = null;

Open();
}
Expand Down Expand Up @@ -781,6 +782,14 @@ internal void OnNotification(object O, NpgsqlNotificationEventArgs E)
}
}

/// <summary>
/// Returns a copy of the NpgsqlConnectionStringBuilder that contains the parsed connection string values.
/// </summary>
internal NpgsqlConnectionStringBuilder CopyConnectionStringBuilder()
{
return settings.Clone();
}

/// <summary>
/// The connector object connected to the backend.
/// </summary>
Expand Down Expand Up @@ -930,12 +939,40 @@ private NpgsqlPromotableSinglePhaseNotification Promotable
/// </summary>
private void LogConnectionString()
{
if (LogLevel.Debug >= NpgsqlEventLog.Level)
return;

foreach (string key in settings.Keys)
{
NpgsqlEventLog.LogMsg(resman, "Log_ConnectionStringValues", LogLevel.Debug, key, settings[key]);
}
}

/// <summary>
/// Sets the `settings` ConnectionStringBuilder based on the given `connectionString`
/// </summary>
/// <param name="connectionString">The connection string to load the builder from</param>
private void LoadConnectionStringBuilder(string connectionString)
{
settings = cache[connectionString];
if (settings == null)
{
settings = new NpgsqlConnectionStringBuilder(connectionString);
cache[connectionString] = settings;
}

RefreshConnectionString();
LogConnectionString();
}

/// <summary>
/// Refresh the cached _connectionString whenever the builder settings change
/// </summary>
private void RefreshConnectionString()
{
_connectionString = settings.ConnectionString;
}

private void CheckConnectionOpen()
{
if (disposed)
Expand Down
12 changes: 6 additions & 6 deletions src/Npgsql/NpgsqlConnector.cs
Expand Up @@ -165,12 +165,12 @@ internal SSPIHandler SSPI
set { _sspi = value; }
}

#endif

public NpgsqlConnector(NpgsqlConnection Connection)
: this(Connection.ConnectionStringValues.Clone(), Connection.Pooling, false)
{}

#endif

public NpgsqlConnector(NpgsqlConnection Connection)
: this(Connection.CopyConnectionStringBuilder(), Connection.Pooling, false)
{}

/// <summary>
/// Constructor.
/// </summary>
Expand Down

0 comments on commit ff0a4b9

Please sign in to comment.