PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework Strange(?) behaviour of SqlCeConnection

Reply

Strange(?) behaviour of SqlCeConnection

 
Thread Tools Rate Thread
Old 11-11-2007, 12:55 AM   #1
Adam
Guest
 
Posts: n/a
Default Strange(?) behaviour of SqlCeConnection


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
Old 11-11-2007, 09:57 PM   #2
=?Utf-8?B?U2ltb24gSGFydCBbTVZQXQ==?=
Guest
 
Posts: n/a
Default RE: Strange(?) behaviour of SqlCeConnection

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off