PC Review


Reply
Thread Tools Rate Thread

Strange(?) behaviour of SqlCeConnection

 
 
Adam
Guest
Posts: n/a
 
      11th Nov 2007
Hello, I observe a strange behaviour of SqlCeConnection disposing.
I have a simple class to connect to DB with one static SqlCeConnection

namespace SOMETHING
{
/// <summary>
/// This singleton class holds the common application values.
/// </summary>
sealed class Common : IDisposable
{
// Singleton instance variable (Values)
public static readonly Common Values = new Common();

private Common()
{
Load();
}

public void Load()
{
this.databaseConnection = new
SqlCeConnection(LocalConnectionString);

}

public void DbOpen()
{
if (this.databaseConnection.State != ConnectionState.Open)
this.databaseConnection.Open();
}

#region Properties
private SqlCeConnection databaseConnection;
public SqlCeConnection DatabaseConnection
{
get { return databaseConnection; }
set { databaseConnection = value; }
}

public string LocalConnectionString
{
get { return "Data Source=" + localDbPath + localDbName + ";
Password = test"; }
}

public string AppPath
{
get { return
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
}
#endregion

#region Dispose
public void Dispose()
{
if(this.databaseConnection.State != ConnectionState.Closed)
this.databaseConnection.Close();
if (this.databaseConnection != null)
{
this.databaseConnection.Dispose();
this.databaseConnection = null;
}
//czy zapisywac zmiany przy usuwaniu obiektu?
//Save();
}
#endregion

~Common()
{
//Dispose();
}
}
}


and from Form1.cs code I do

Common.Values.DbOpen();

in Form1 on load event
and

Common.Values.Dispose();

in Form1 closing event
and everything is ok. But when I uncomment Dispose in Common destructor and
not use Common.Values.Dispose() in Form1 closing event application hang on
this.databaseConnection.Close() in Dispose function. Why?
 
Reply With Quote
 
 
 
 
=?Utf-8?B?U2ltb24gSGFydCBbTVZQXQ==?=
Guest
Posts: n/a
 
      11th Nov 2007
This looks as if you have somekind of locking problem I would guess. To be
honest I wouldn't implement a Finalizer in this case. If you were to call
Dispose from your client app, you might have a synchronization issue.
Consider the following: You call Dispose, then the object becomes eligible
for clean up, so the GC calls your Finalizer, you now have two threads in
your Dispose method. This is very unlikely because the timing would have to
be just right. If you still want a Finalizer, consider calling
GC.SuppressFinalize(this) in your dispose method to limit a double clean up.

I would recomend using the *using* statement and keep the Dispose method.
Once control leaves the using statement the Dispose method is called for you.
To to this though, you'd of course have to loose the singleton pattern!
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com


"Adam" wrote:

> Hello, I observe a strange behaviour of SqlCeConnection disposing.
> I have a simple class to connect to DB with one static SqlCeConnection
>
> namespace SOMETHING
> {
> /// <summary>
> /// This singleton class holds the common application values.
> /// </summary>
> sealed class Common : IDisposable
> {
> // Singleton instance variable (Values)
> public static readonly Common Values = new Common();
>
> private Common()
> {
> Load();
> }
>
> public void Load()
> {
> this.databaseConnection = new
> SqlCeConnection(LocalConnectionString);
>
> }
>
> public void DbOpen()
> {
> if (this.databaseConnection.State != ConnectionState.Open)
> this.databaseConnection.Open();
> }
>
> #region Properties
> private SqlCeConnection databaseConnection;
> public SqlCeConnection DatabaseConnection
> {
> get { return databaseConnection; }
> set { databaseConnection = value; }
> }
>
> public string LocalConnectionString
> {
> get { return "Data Source=" + localDbPath + localDbName + ";
> Password = test"; }
> }
>
> public string AppPath
> {
> get { return
> Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
> }
> }
> #endregion
>
> #region Dispose
> public void Dispose()
> {
> if(this.databaseConnection.State != ConnectionState.Closed)
> this.databaseConnection.Close();
> if (this.databaseConnection != null)
> {
> this.databaseConnection.Dispose();
> this.databaseConnection = null;
> }
> //czy zapisywac zmiany przy usuwaniu obiektu?
> //Save();
> }
> #endregion
>
> ~Common()
> {
> //Dispose();
> }
> }
> }
>
>
> and from Form1.cs code I do
>
> Common.Values.DbOpen();
>
> in Form1 on load event
> and
>
> Common.Values.Dispose();
>
> in Form1 closing event
> and everything is ok. But when I uncomment Dispose in Common destructor and
> not use Common.Values.Dispose() in Form1 closing event application hang on
> this.databaseConnection.Close() in Dispose function. Why?
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behaviour =?Utf-8?B?UGhpbA==?= Windows XP Help 1 20th Aug 2005 11:42 PM
Strange Behaviour kp Microsoft Windows 2000 Windows Updates 1 19th Aug 2005 02:42 AM
strange ewf behaviour =?Utf-8?B?Ry5Sb2VsYW50?= Windows XP Embedded 6 4th Jan 2005 03:46 PM
IE6 Strange Behaviour Peter Windows XP Internet Explorer 3 2nd Oct 2004 05:48 PM
Strange behaviour - please help Alan Microsoft Excel Discussion 4 25th Mar 2004 06:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:44 PM.