Showing posts with label writing. Show all posts
Showing posts with label writing. Show all posts

Saturday, March 24, 2012

this.Connection.Close(); does not close the Oracle session! Pleasehelp!

Hi all!

I'm writing an ASP.NET web application that uses an Oracle database.

I OPEN the Oracle connection by using the following code:

if (this.ConnectionString != "")
{
this.Connection = new OracleConnection(this.ConnectionString);
this.Connection.Open();
return;
}

I then create the command object by using the following:

OracleCommand DBCmd = new
OracleCommand("PRL_STORE_PAYMENT_REQ_PKG.GET_PAYMENT_REQ_LOOKUP",
objConnect.Connection);
DBCmd.CommandType = System.Data.CommandType.StoredProcedure;

I then I run the OracleCommand.ExecuteReader method to execute the
stored procedure.

I then CLOSE the connection by using the
this.Connection.Close(); command.

However, the sessions stay active!!!
I have a very angry DBA on my tail about this one!
Please help!

Thanks
SteveI'm pretty sure "OracleConnection" is not a standard Framework class. You'll
probably have better luck asking the component vendor (or in their forums).
My guess is that it has to do with connection pooling. Do the number of
connections grow endlessly, or are they reused over and over? If it's the
later, then you should actually have a very happy DBA.

Scott

"S_K" <steve_kershaw@.yahoo.comwrote in message
news:3619d1f2-ef79-41cb-a0af-5df58dd223a0@.e10g2000prf.googlegroups.com...

Quote:

Originally Posted by

Hi all!
>
I'm writing an ASP.NET web application that uses an Oracle database.
>
I OPEN the Oracle connection by using the following code:
>
if (this.ConnectionString != "")
{
this.Connection = new OracleConnection(this.ConnectionString);
this.Connection.Open();
return;
}
>
I then create the command object by using the following:
>
OracleCommand DBCmd = new
OracleCommand("PRL_STORE_PAYMENT_REQ_PKG.GET_PAYMENT_REQ_LOOKUP",
objConnect.Connection);
DBCmd.CommandType = System.Data.CommandType.StoredProcedure;
>
I then I run the OracleCommand.ExecuteReader method to execute the
stored procedure.
>
I then CLOSE the connection by using the
this.Connection.Close(); command.
>
However, the sessions stay active!!!
I have a very angry DBA on my tail about this one!
Please help!
>
Thanks
Steve


Did you close the reader?

if(null!=reader)
{
reader.Close();
}

?

A reader is a live firehouse...thus you use it, then you tidy up.
You can't get reader, close the connection , then use the reader. That
doesn't make sense.

Use the reader as quickly as possible, then close it.

"S_K" <steve_kershaw@.yahoo.comwrote in message
news:3619d1f2-ef79-41cb-a0af-5df58dd223a0@.e10g2000prf.googlegroups.com...

Quote:

Originally Posted by

Hi all!
>
I'm writing an ASP.NET web application that uses an Oracle database.
>
I OPEN the Oracle connection by using the following code:
>
if (this.ConnectionString != "")
{
this.Connection = new OracleConnection(this.ConnectionString);
this.Connection.Open();
return;
}
>
I then create the command object by using the following:
>
OracleCommand DBCmd = new
OracleCommand("PRL_STORE_PAYMENT_REQ_PKG.GET_PAYMENT_REQ_LOOKUP",
objConnect.Connection);
DBCmd.CommandType = System.Data.CommandType.StoredProcedure;
>
I then I run the OracleCommand.ExecuteReader method to execute the
stored procedure.
>
I then CLOSE the connection by using the
this.Connection.Close(); command.
>
However, the sessions stay active!!!
I have a very angry DBA on my tail about this one!
Please help!
>
Thanks
Steve


you will need to turn connection pooling off (see your connect string
properties). with pooling on, connection.close only returns the connection to
the pool, it does not close it. you may make your dba happy by lowing the
pool timeout.

-- bruce (sqlwork.com)

"S_K" wrote:

Quote:

Originally Posted by

Hi all!
>
I'm writing an ASP.NET web application that uses an Oracle database.
>
I OPEN the Oracle connection by using the following code:
>
if (this.ConnectionString != "")
{
this.Connection = new OracleConnection(this.ConnectionString);
this.Connection.Open();
return;
}
>
I then create the command object by using the following:
>
OracleCommand DBCmd = new
OracleCommand("PRL_STORE_PAYMENT_REQ_PKG.GET_PAYMENT_REQ_LOOKUP",
objConnect.Connection);
DBCmd.CommandType = System.Data.CommandType.StoredProcedure;
>
I then I run the OracleCommand.ExecuteReader method to execute the
stored procedure.
>
I then CLOSE the connection by using the
this.Connection.Close(); command.
>
However, the sessions stay active!!!
I have a very angry DBA on my tail about this one!
Please help!
>
Thanks
Steve
>


On Dec 6, 3:13 pm, bruce barker
<brucebar...@.discussions.microsoft.comwrote:

Quote:

Originally Posted by

you will need to turn connection pooling off (see your connect string
properties). with pooling on, connection.close only returns the connection to
the pool, it does not close it. you may make your dba happy by lowing the
pool timeout.
>
-- bruce (sqlwork.com)
>
>
>
"S_K" wrote:

Quote:

Originally Posted by

Hi all!


>

Quote:

Originally Posted by

I'm writing an ASP.NET web application that uses an Oracle database.


>

Quote:

Originally Posted by

I OPEN the Oracle connection by using the following code:


>

Quote:

Originally Posted by

if (this.ConnectionString != "")
{
this.Connection = new OracleConnection(this.ConnectionString);
this.Connection.Open();
return;
}


>

Quote:

Originally Posted by

I then create the command object by using the following:


>

Quote:

Originally Posted by

OracleCommand DBCmd = new
OracleCommand("PRL_STORE_PAYMENT_REQ_PKG.GET_PAYMENT_REQ_LOOKUP",
objConnect.Connection);
DBCmd.CommandType = System.Data.CommandType.StoredProcedure;


>

Quote:

Originally Posted by

I then I run the OracleCommand.ExecuteReader method to execute the
stored procedure.


>

Quote:

Originally Posted by

I then CLOSE the connection by using the
this.Connection.Close(); command.


>

Quote:

Originally Posted by

However, the sessions stay active!!!
I have a very angry DBA on my tail about this one!
Please help!


>

Quote:

Originally Posted by

Thanks
Steve- Hide quoted text -


>
- Show quoted text -


I don't actually CLOSE the reader but I DO Dispose the reader:
reader.Dispose();

Doesn't that close the reader as well?

Also, how do you turn off the connection pooling in the connection
string? All I have is the basic connection string.

Thanks for your quick replies.

Steve
no, dispose will return it to the pool if pooling is enabled (all dispose
does is call close if needed).

you will need the connection string settings docs for the driver you are
using..

-- bruce (sqlwork.com)

"S_K" wrote:

Quote:

Originally Posted by

On Dec 6, 3:13 pm, bruce barker
<brucebar...@.discussions.microsoft.comwrote:

Quote:

Originally Posted by

you will need to turn connection pooling off (see your connect string
properties). with pooling on, connection.close only returns the connection to
the pool, it does not close it. you may make your dba happy by lowing the
pool timeout.

-- bruce (sqlwork.com)

"S_K" wrote:

Quote:

Originally Posted by

Hi all!


Quote:

Originally Posted by

I'm writing an ASP.NET web application that uses an Oracle database.


Quote:

Originally Posted by

I OPEN the Oracle connection by using the following code:


Quote:

Originally Posted by

if (this.ConnectionString != "")
{
this.Connection = new OracleConnection(this.ConnectionString);
this.Connection.Open();
return;
}


Quote:

Originally Posted by

I then create the command object by using the following:


Quote:

Originally Posted by

OracleCommand DBCmd = new
OracleCommand("PRL_STORE_PAYMENT_REQ_PKG.GET_PAYMENT_REQ_LOOKUP",
objConnect.Connection);
DBCmd.CommandType = System.Data.CommandType.StoredProcedure;


Quote:

Originally Posted by

I then I run the OracleCommand.ExecuteReader method to execute the
stored procedure.


Quote:

Originally Posted by

I then CLOSE the connection by using the
this.Connection.Close(); command.


Quote:

Originally Posted by

However, the sessions stay active!!!
I have a very angry DBA on my tail about this one!
Please help!


Quote:

Originally Posted by

Thanks
Steve- Hide quoted text -


- Show quoted text -


>
I don't actually CLOSE the reader but I DO Dispose the reader:
reader.Dispose();
>
Doesn't that close the reader as well?
>
Also, how do you turn off the connection pooling in the connection
string? All I have is the basic connection string.
>
Thanks for your quick replies.
>
Steve
>


OracleConnection is in the System.Data.OracleClient namespace.

Although Oracle also provides such a class, in their
Oracle.DataAccess.Client namespace.

If the connection pool advice doesn't work out, try also calling
Dispose on the connection. Unless the issue has been resolved in a
recent version, there's a flaw in the Framework class that
necessitates this. And it can't hurt in any event.

On Dec 6, 4:52 pm, "Scott Roberts" <srobe...@.no.spam.here-webworks-
software.comwrote:

Quote:

Originally Posted by

I'm pretty sure "OracleConnection" is not a standard Framework class. You'll
probably have better luck asking the component vendor (or in their forums).
My guess is that it has to do with connection pooling. Do the number of
connections grow endlessly, or are they reused over and over? If it's the
later, then you should actually have a very happy DBA.
>
Scott
>
"S_K" <steve_kers...@.yahoo.comwrote in message
>
news:3619d1f2-ef79-41cb-a0af-5df58dd223a0@.e10g2000prf.googlegroups.com...
>
>
>

Quote:

Originally Posted by

Hi all!


>

Quote:

Originally Posted by

I'm writing an ASP.NET web application that uses an Oracle database.


>

Quote:

Originally Posted by

I OPEN the Oracle connection by using the following code:


>

Quote:

Originally Posted by

if (this.ConnectionString != "")
{
this.Connection = new OracleConnection(this.ConnectionString);
this.Connection.Open();
return;
}


>

Quote:

Originally Posted by

I then create the command object by using the following:


>

Quote:

Originally Posted by

OracleCommand DBCmd = new
OracleCommand("PRL_STORE_PAYMENT_REQ_PKG.GET_PAYMENT_REQ_LOOKUP",
objConnect.Connection);
DBCmd.CommandType = System.Data.CommandType.StoredProcedure;


>

Quote:

Originally Posted by

I then I run the OracleCommand.ExecuteReader method to execute the
stored procedure.


>

Quote:

Originally Posted by

I then CLOSE the connection by using the
this.Connection.Close(); command.


>

Quote:

Originally Posted by

However, the sessions stay active!!!
I have a very angry DBA on my tail about this one!
Please help!


>

Quote:

Originally Posted by

Thanks
Steve- Hide quoted text -


>
- Show quoted text -

this.Connection.Close(); does not close the Oracle session! Please

Hi all!
I'm writing an ASP.NET web application that uses an Oracle database.
I OPEN the Oracle connection by using the following code:
if (this.ConnectionString != "")
{
this.Connection = new OracleConnection(this.ConnectionString);
this.Connection.Open();
return;
}
I then create the command object by using the following:
OracleCommand DBCmd = new
OracleCommand("PRL_STORE_PAYMENT_REQ_PKG.GET_PAYMENT_REQ_LOOKUP",
objConnect.Connection);
DBCmd.CommandType = System.Data.CommandType.StoredProcedure;
I then I run the OracleCommand.ExecuteReader method to execute the
stored procedure.
I then CLOSE the connection by using the
this.Connection.Close(); command.
However, the sessions stay active!!!
I have a very DBA on my tail about this one!
Please help!
Thanks
Steveyou will need to turn connection pooling off (see your connect string
properties). with pooling on, connection.close only returns the connection t
o
the pool, it does not close it. you may make your dba happy by lowing the
pool timeout.
-- bruce (sqlwork.com)
"S_K" wrote:

> Hi all!
> I'm writing an ASP.NET web application that uses an Oracle database.
> I OPEN the Oracle connection by using the following code:
> if (this.ConnectionString != "")
> {
> this.Connection = new OracleConnection(this.ConnectionString);
> this.Connection.Open();
> return;
> }
> I then create the command object by using the following:
> OracleCommand DBCmd = new
> OracleCommand("PRL_STORE_PAYMENT_REQ_PKG.GET_PAYMENT_REQ_LOOKUP",
> objConnect.Connection);
> DBCmd.CommandType = System.Data.CommandType.StoredProcedure;
> I then I run the OracleCommand.ExecuteReader method to execute the
> stored procedure.
> I then CLOSE the connection by using the
> this.Connection.Close(); command.
> However, the sessions stay active!!!
> I have a very DBA on my tail about this one!
> Please help!
> Thanks
> Steve
>

Thursday, March 22, 2012

Thrash my site

Note: "Thrash" not "Trash" :-)
I've developed a website, the purpose of writing this site was to test a
Model Driven Architecture type approach.
http://www.howtodothings.com
I'm not sure how well this approach stands up to high-demand. I thought I'd
ask in here if as many people as possible could just flick through some
categories, pull up some articles, etc.
Now that I think of it though this is a bit of a crappy way to test it, no
co-ordination of the demand. In which case, what is a good way to stress
test a website?
Thanks
Pete
====
ECO Modeler, Audio compression components, DIB graphics controls,
FastStrings
http://www.droopyeyes.com
Read or write articles on just about anything
http://www.HowToDoThings.com
My blog
http://blogs.slcdug.org/petermorris/Try using http://jakarta.apache.org/jmeter/
Its a good tool for testing websites, it requires java runtime environment
since its written in java, but it can test ASP pages just fine.
/Dan
"Peter Morris [Droopy eyes software]" <pete@.not.this.or.this.droopyeyes.com>
wrote in message news:%235KAFt4AFHA.2640@.TK2MSFTNGP14.phx.gbl...
> Note: "Thrash" not "Trash" :-)
> Now that I think of it though this is a bit of a crappy way to test it, no
> co-ordination of the demand. In which case, what is a good way to stress
> test a website?
>
If you're using IIS5.x, you can use WAST
( Web Application Stress Tool ) :
http://www.microsoft.com/downloads/...&displaylang=en
If you're using IIS 6.0, you can use the
Web Capacity Analysis Tool Version 5.2,
included in the IIS 6.0 Resource Kit:
http://www.microsoft.com/downloads/...&displaylang=en
Both tools do a bangup job of simulating multiple
browsers requesting pages from a web site
Juan T. Llibre
ASP.NET MVP
===========
"Peter Morris [Droopy eyes software]" <pete@.not.this.or.this.droopyeyes.com>
wrote in message news:%235KAFt4AFHA.2640@.TK2MSFTNGP14.phx.gbl...
> Note: "Thrash" not "Trash" :-)
> I've developed a website, the purpose of writing this site was to test a
> Model Driven Architecture type approach.
> http://www.howtodothings.com
> I'm not sure how well this approach stands up to high-demand. I thought
> I'd ask in here if as many people as possible could just flick through
> some categories, pull up some articles, etc.
> Now that I think of it though this is a bit of a crappy way to test it, no
> co-ordination of the demand. In which case, what is a good way to stress
> test a website?
> Thanks
>
> --
> Pete
> ====
> ECO Modeler, Audio compression components, DIB graphics controls,
> FastStrings
> http://www.droopyeyes.com
> Read or write articles on just about anything
> http://www.HowToDoThings.com
> My blog
> http://blogs.slcdug.org/petermorris/
>

Thrash my site

Note: "Thrash" not "Trash" :-)

I've developed a website, the purpose of writing this site was to test a
Model Driven Architecture type approach.
http://www.howtodothings.com

I'm not sure how well this approach stands up to high-demand. I thought I'd
ask in here if as many people as possible could just flick through some
categories, pull up some articles, etc.

Now that I think of it though this is a bit of a crappy way to test it, no
co-ordination of the demand. In which case, what is a good way to stress
test a website?

Thanks

--
Pete
====
ECO Modeler, Audio compression components, DIB graphics controls,
FastStrings
http://www.droopyeyes.com

Read or write articles on just about anything
http://www.HowToDoThings.com

My blog
http://blogs.slcdug.org/petermorris/Try using http://jakarta.apache.org/jmeter/
Its a good tool for testing websites, it requires java runtime environment
since its written in java, but it can test ASP pages just fine.

/Dan

"Peter Morris [Droopy eyes software]" <pete@.not.this.or.this.droopyeyes.com>
wrote in message news:%235KAFt4AFHA.2640@.TK2MSFTNGP14.phx.gbl...
> Note: "Thrash" not "Trash" :-)
> Now that I think of it though this is a bit of a crappy way to test it, no
> co-ordination of the demand. In which case, what is a good way to stress
> test a website?
If you're using IIS5.x, you can use WAST
( Web Application Stress Tool ) :

http://www.microsoft.com/downloads/...&displaylang=en

If you're using IIS 6.0, you can use the
Web Capacity Analysis Tool Version 5.2,
included in the IIS 6.0 Resource Kit:

http://www.microsoft.com/downloads/...&displaylang=en

Both tools do a bangup job of simulating multiple
browsers requesting pages from a web site

Juan T. Llibre
ASP.NET MVP
===========
"Peter Morris [Droopy eyes software]" <pete@.not.this.or.this.droopyeyes.com>
wrote in message news:%235KAFt4AFHA.2640@.TK2MSFTNGP14.phx.gbl...
> Note: "Thrash" not "Trash" :-)
> I've developed a website, the purpose of writing this site was to test a
> Model Driven Architecture type approach.
> http://www.howtodothings.com
> I'm not sure how well this approach stands up to high-demand. I thought
> I'd ask in here if as many people as possible could just flick through
> some categories, pull up some articles, etc.
> Now that I think of it though this is a bit of a crappy way to test it, no
> co-ordination of the demand. In which case, what is a good way to stress
> test a website?
> Thanks
>
> --
> Pete
> ====
> ECO Modeler, Audio compression components, DIB graphics controls,
> FastStrings
> http://www.droopyeyes.com
> Read or write articles on just about anything
> http://www.HowToDoThings.com
> My blog
> http://blogs.slcdug.org/petermorris/

Tuesday, March 13, 2012

Thread safety advice

My application uses a singleton static class for writing entries to a log
file. The location and name of the log-file is read from web.config each time
an entry is written, but has the current date inserted into its name. For
example, the string:

c:\inetpub\wwwroot\myapp\log.txt

will be changed to:

c:\inetpub\wwwroot\myapp\log_20060216.txt

However, as we have many concurrent users on the site at once, each
generating hundreds of log file entries a minute, we're finding that multiple
threads are calling the same code at the same time and we're getting filename
with multiple date stamps. Ie:

c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt

I'm sure this is to do with the following code not being thread-safe despite
the "lock" critical section statement:

string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];

lock (FoLockObject) {
sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(sLog_File_Path));
}

Can anyone offer advice on the correct way to protect code that is
vulnerable to threading issues. I'm aware of synchronization objects such as
Mutex's but am unsure how to use them with static classes. A stright forward
example in C# without being too clever would be very handy

Thanks

BenIs sLog_File_Path local or member variable?
What about FoLockObject?

Unfortunately it' hard to say where is the problem you need to show us full
class.

George.

"Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
> My application uses a singleton static class for writing entries to a log
> file. The location and name of the log-file is read from web.config each
> time
> an entry is written, but has the current date inserted into its name. For
> example, the string:
> c:\inetpub\wwwroot\myapp\log.txt
> will be changed to:
> c:\inetpub\wwwroot\myapp\log_20060216.txt
> However, as we have many concurrent users on the site at once, each
> generating hundreds of log file entries a minute, we're finding that
> multiple
> threads are calling the same code at the same time and we're getting
> filename
> with multiple date stamps. Ie:
> c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt
> I'm sure this is to do with the following code not being thread-safe
> despite
> the "lock" critical section statement:
> string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];
> lock (FoLockObject) {
> sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
> Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(sLog_File_Path));
> }
> Can anyone offer advice on the correct way to protect code that is
> vulnerable to threading issues. I'm aware of synchronization objects such
> as
> Mutex's but am unsure how to use them with static classes. A stright
> forward
> example in C# without being too clever would be very handy
> Thanks
> Ben
Here's an abbreviated version of the full class.

public class CLog {
private static string FsLog_File_Path = "";
private static object FoLockObject = new object();

private static void RefreshSettings() {
FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];

lock (FoLockObject) {
FsLog_File_Path =
Path.Combine(Path.GetDirectoryName(FsLog_File_Path ),
Path.GetFileNameWithoutExtension(FsLog_File_Path) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(FsLog_File_Path));
}
}

public static void WriteInfo(string sDescription) {
RefreshSettings();

StreamWriter oStream = null;

try {
if (File.Exists(FsLog_File_Path)) {
oStream = File.AppendText(FsLog_File_Path);
}
else {
oStream = File.CreateText(FsLog_File_Path);
}

oStream.AutoFlush = true;

string sSession = "";
if (HttpContext.Current != null) sSession =
HttpContext.Current.Session.SessionID + " - ";
else sSession = "";

oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
sDescription));
}
catch (IOException) {

}
finally {
if (oStream != null) oStream.Close();
oStream = null;
}
}
}

"George Ter-Saakov" wrote:

> Is sLog_File_Path local or member variable?
> What about FoLockObject?
> Unfortunately it' hard to say where is the problem you need to show us full
> class.
> George.
>
> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
> news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
> > My application uses a singleton static class for writing entries to a log
> > file. The location and name of the log-file is read from web.config each
> > time
> > an entry is written, but has the current date inserted into its name. For
> > example, the string:
> > c:\inetpub\wwwroot\myapp\log.txt
> > will be changed to:
> > c:\inetpub\wwwroot\myapp\log_20060216.txt
> > However, as we have many concurrent users on the site at once, each
> > generating hundreds of log file entries a minute, we're finding that
> > multiple
> > threads are calling the same code at the same time and we're getting
> > filename
> > with multiple date stamps. Ie:
> > c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt
> > I'm sure this is to do with the following code not being thread-safe
> > despite
> > the "lock" critical section statement:
> > string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];
> > lock (FoLockObject) {
> > sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
> > Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
> > DateTime.Now.ToString("yyyyMMdd") +
> > Path.GetExtension(sLog_File_Path));
> > }
> > Can anyone offer advice on the correct way to protect code that is
> > vulnerable to threading issues. I'm aware of synchronization objects such
> > as
> > Mutex's but am unsure how to use them with static classes. A stright
> > forward
> > example in C# without being too clever would be very handy
> > Thanks
> > Ben
>
You do have a problem in your code.
This line is not thread safe.

FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];

You are modifying the variable while some other thread could have been doing
the code that is in lock {} section.
So move that line inside (but read further)

---------------
I am not sure why your are doing it this way. Because I do not see any gain
in FsLog_File_Path beign global/member variable.
I would rewrite the code to avoid any synchronization

private static void RefreshSettings() {
string sPath =
ConfigurationSettings.AppSettings["logfilepath"];
sPath = Path.Combine(Path.GetDirectoryName(sPath),
Path.GetFileNameWithoutExtension(sPath) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(sPath));
}
}

As you can see all manipulations are made to local variable so you do not
need lock.

--------------------

The only place where you will need to lock is where you are opening the file
and writing to it. Since that is not thread safe.
And this will fail if you try to open/write into file from multiple threads.

George

"Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
news:B6D5463F-61F6-41BB-B39D-7679E2587C0A@.microsoft.com...
> Here's an abbreviated version of the full class.
> public class CLog {
> private static string FsLog_File_Path = "";
> private static object FoLockObject = new object();
> private static void RefreshSettings() {
> FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
> lock (FoLockObject) {
> FsLog_File_Path =
> Path.Combine(Path.GetDirectoryName(FsLog_File_Path ),
> Path.GetFileNameWithoutExtension(FsLog_File_Path) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(FsLog_File_Path));
> }
> }
> public static void WriteInfo(string sDescription) {
> RefreshSettings();
> StreamWriter oStream = null;
> try {
> if (File.Exists(FsLog_File_Path)) {
> oStream = File.AppendText(FsLog_File_Path);
> }
> else {
> oStream = File.CreateText(FsLog_File_Path);
> }
> oStream.AutoFlush = true;
> string sSession = "";
> if (HttpContext.Current != null) sSession =
> HttpContext.Current.Session.SessionID + " - ";
> else sSession = "";
>
> oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
> DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
> sDescription));
> }
> catch (IOException) {
> }
> finally {
> if (oStream != null) oStream.Close();
> oStream = null;
> }
> }
> }
> "George Ter-Saakov" wrote:
>> Is sLog_File_Path local or member variable?
>> What about FoLockObject?
>>
>> Unfortunately it' hard to say where is the problem you need to show us
>> full
>> class.
>>
>> George.
>>
>>
>> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
>> news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
>> > My application uses a singleton static class for writing entries to a
>> > log
>> > file. The location and name of the log-file is read from web.config
>> > each
>> > time
>> > an entry is written, but has the current date inserted into its name.
>> > For
>> > example, the string:
>>> > c:\inetpub\wwwroot\myapp\log.txt
>>> > will be changed to:
>>> > c:\inetpub\wwwroot\myapp\log_20060216.txt
>>> > However, as we have many concurrent users on the site at once, each
>> > generating hundreds of log file entries a minute, we're finding that
>> > multiple
>> > threads are calling the same code at the same time and we're getting
>> > filename
>> > with multiple date stamps. Ie:
>>> > c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt
>>> > I'm sure this is to do with the following code not being thread-safe
>> > despite
>> > the "lock" critical section statement:
>>> > string sLog_File_Path =
>> > ConfigurationManager.AppSettings["logfilepath"];
>>> > lock (FoLockObject) {
>> > sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
>> > Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
>> > DateTime.Now.ToString("yyyyMMdd") +
>> > Path.GetExtension(sLog_File_Path));
>> > }
>>> > Can anyone offer advice on the correct way to protect code that is
>> > vulnerable to threading issues. I'm aware of synchronization objects
>> > such
>> > as
>> > Mutex's but am unsure how to use them with static classes. A stright
>> > forward
>> > example in C# without being too clever would be very handy
>>> > Thanks
>>> > Ben
>>>
>>
>
Hi George,

I see the error of my ways and have changed it as you suggested. This was
basically a quick hack to include the date in the filename, as this wasn't
the origianl intention.

Thanks for the advice

Ben

"George Ter-Saakov" <gt-nsp@.cardone.com> wrote in message
news:OOv8TCwMGHA.3732@.TK2MSFTNGP10.phx.gbl...
> You do have a problem in your code.
> This line is not thread safe.
> FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
> You are modifying the variable while some other thread could have been
> doing the code that is in lock {} section.
> So move that line inside (but read further)
> ---------------
> I am not sure why your are doing it this way. Because I do not see any
> gain in FsLog_File_Path beign global/member variable.
> I would rewrite the code to avoid any synchronization
> private static void RefreshSettings() {
> string sPath =
> ConfigurationSettings.AppSettings["logfilepath"];
> sPath = Path.Combine(Path.GetDirectoryName(sPath),
> Path.GetFileNameWithoutExtension(sPath) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(sPath));
> }
> }
>
> As you can see all manipulations are made to local variable so you do not
> need lock.
> --------------------
> The only place where you will need to lock is where you are opening the
> file and writing to it. Since that is not thread safe.
> And this will fail if you try to open/write into file from multiple
> threads.
> George
>
> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
> news:B6D5463F-61F6-41BB-B39D-7679E2587C0A@.microsoft.com...
>> Here's an abbreviated version of the full class.
>>
>> public class CLog {
>> private static string FsLog_File_Path = "";
>> private static object FoLockObject = new object();
>>
>> private static void RefreshSettings() {
>> FsLog_File_Path =
>> ConfigurationSettings.AppSettings["logfilepath"];
>>
>> lock (FoLockObject) {
>> FsLog_File_Path =
>> Path.Combine(Path.GetDirectoryName(FsLog_File_Path ),
>> Path.GetFileNameWithoutExtension(FsLog_File_Path) + "_" +
>> DateTime.Now.ToString("yyyyMMdd") +
>> Path.GetExtension(FsLog_File_Path));
>> }
>> }
>>
>> public static void WriteInfo(string sDescription) {
>> RefreshSettings();
>>
>> StreamWriter oStream = null;
>>
>> try {
>> if (File.Exists(FsLog_File_Path)) {
>> oStream = File.AppendText(FsLog_File_Path);
>> }
>> else {
>> oStream = File.CreateText(FsLog_File_Path);
>> }
>>
>> oStream.AutoFlush = true;
>>
>> string sSession = "";
>> if (HttpContext.Current != null) sSession =
>> HttpContext.Current.Session.SessionID + " - ";
>> else sSession = "";
>>
>>
>> oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
>> DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
>> sDescription));
>> }
>> catch (IOException) {
>>
>> }
>> finally {
>> if (oStream != null) oStream.Close();
>> oStream = null;
>> }
>> }
>> }
>>
>> "George Ter-Saakov" wrote:
>>
>>> Is sLog_File_Path local or member variable?
>>> What about FoLockObject?
>>>
>>> Unfortunately it' hard to say where is the problem you need to show us
>>> full
>>> class.
>>>
>>> George.
>>>
>>>
>>> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
>>> news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
>>> > My application uses a singleton static class for writing entries to a
>>> > log
>>> > file. The location and name of the log-file is read from web.config
>>> > each
>>> > time
>>> > an entry is written, but has the current date inserted into its name.
>>> > For
>>> > example, the string:
>>>>> > c:\inetpub\wwwroot\myapp\log.txt
>>>>> > will be changed to:
>>>>> > c:\inetpub\wwwroot\myapp\log_20060216.txt
>>>>> > However, as we have many concurrent users on the site at once, each
>>> > generating hundreds of log file entries a minute, we're finding that
>>> > multiple
>>> > threads are calling the same code at the same time and we're getting
>>> > filename
>>> > with multiple date stamps. Ie:
>>>>> > c:\inetpub\wwwroot\myapp\log_20060216_20060216.txt
>>>>> > I'm sure this is to do with the following code not being thread-safe
>>> > despite
>>> > the "lock" critical section statement:
>>>>> > string sLog_File_Path =
>>> > ConfigurationManager.AppSettings["logfilepath"];
>>>>> > lock (FoLockObject) {
>>> > sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path) ,
>>> > Path.GetFileNameWithoutExtension(sLog_File_Path) + "_" +
>>> > DateTime.Now.ToString("yyyyMMdd") +
>>> > Path.GetExtension(sLog_File_Path));
>>> > }
>>>>> > Can anyone offer advice on the correct way to protect code that is
>>> > vulnerable to threading issues. I'm aware of synchronization objects
>>> > such
>>> > as
>>> > Mutex's but am unsure how to use them with static classes. A stright
>>> > forward
>>> > example in C# without being too clever would be very handy
>>>>> > Thanks
>>>>> > Ben
>>>>>
>>>
>>>

Thread safety advice

My application uses a singleton static class for writing entries to a log
file. The location and name of the log-file is read from web.config each tim
e
an entry is written, but has the current date inserted into its name. For
example, the string:
c:\inetpub\wwwroot\myapp\log.txt
will be changed to:
c:\inetpub\wwwroot\myapp\log_20060216.txt
However, as we have many concurrent users on the site at once, each
generating hundreds of log file entries a minute, we're finding that multipl
e
threads are calling the same code at the same time and we're getting filenam
e
with multiple date stamps. Ie:
c:\inetpub\wwwroot\myapp\log_20060216_20
060216.txt
I'm sure this is to do with the following code not being thread-safe despite
the "lock" critical section statement:
string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];
lock (FoLockObject) {
sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path),
Path. GetFileNameWithoutExtension(sLog_File_Pa
th) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(sLog_File_Path));
}
Can anyone offer advice on the correct way to protect code that is
vulnerable to threading issues. I'm aware of synchronization objects such as
Mutex's but am unsure how to use them with static classes. A stright forward
example in C# without being too clever would be very handy
Thanks
BenIs sLog_File_Path local or member variable?
What about FoLockObject?
Unfortunately it' hard to say where is the problem you need to show us full
class.
George.
"Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
> My application uses a singleton static class for writing entries to a log
> file. The location and name of the log-file is read from web.config each
> time
> an entry is written, but has the current date inserted into its name. For
> example, the string:
> c:\inetpub\wwwroot\myapp\log.txt
> will be changed to:
> c:\inetpub\wwwroot\myapp\log_20060216.txt
> However, as we have many concurrent users on the site at once, each
> generating hundreds of log file entries a minute, we're finding that
> multiple
> threads are calling the same code at the same time and we're getting
> filename
> with multiple date stamps. Ie:
> c:\inetpub\wwwroot\myapp\log_20060216_20
060216.txt
> I'm sure this is to do with the following code not being thread-safe
> despite
> the "lock" critical section statement:
> string sLog_File_Path = ConfigurationManager.AppSettings["logfilepath"];
> lock (FoLockObject) {
> sLog_File_Path = Path.Combine(Path.GetDirectoryName(sLog_File_Path),
> Path. GetFileNameWithoutExtension(sLog_File_Pa
th) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(sLog_File_Path));
> }
> Can anyone offer advice on the correct way to protect code that is
> vulnerable to threading issues. I'm aware of synchronization objects such
> as
> Mutex's but am unsure how to use them with static classes. A stright
> forward
> example in C# without being too clever would be very handy
> Thanks
> Ben
>
Here's an abbreviated version of the full class.
public class CLog {
private static string FsLog_File_Path = "";
private static object FoLockObject = new object();
private static void RefreshSettings() {
FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
lock (FoLockObject) {
FsLog_File_Path =
Path.Combine(Path.GetDirectoryName(FsLog_File_Path),
Path. GetFileNameWithoutExtension(FsLog_File_P
ath) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(FsLog_File_Path));
}
}
public static void WriteInfo(string sDescription) {
RefreshSettings();
StreamWriter oStream = null;
try {
if (File.Exists(FsLog_File_Path)) {
oStream = File.AppendText(FsLog_File_Path);
}
else {
oStream = File.CreateText(FsLog_File_Path);
}
oStream.AutoFlush = true;
string sSession = "";
if (HttpContext.Current != null) sSession =
HttpContext.Current.Session.SessionID + " - ";
else sSession = "";
oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
sDescription));
}
catch (IOException) {
}
finally {
if (oStream != null) oStream.Close();
oStream = null;
}
}
}
"George Ter-Saakov" wrote:

> Is sLog_File_Path local or member variable?
> What about FoLockObject?
> Unfortunately it' hard to say where is the problem you need to show us fu
ll
> class.
> George.
>
> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
> news:27A2FF7B-4565-40A5-8D95-CE197DE94166@.microsoft.com...
>
>
You do have a problem in your code.
This line is not thread safe.
FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
You are modifying the variable while some other thread could have been doing
the code that is in lock {} section.
So move that line inside (but read further)
---
I am not sure why your are doing it this way. Because I do not see any gain
in FsLog_File_Path beign global/member variable.
I would rewrite the code to avoid any synchronization
private static void RefreshSettings() {
string sPath =
ConfigurationSettings.AppSettings["logfilepath"];
sPath = Path.Combine(Path.GetDirectoryName(sPath),
Path.GetFileNameWithoutExtension(sPath) + "_" +
DateTime.Now.ToString("yyyyMMdd") +
Path.GetExtension(sPath));
}
}
As you can see all manipulations are made to local variable so you do not
need lock.
----
The only place where you will need to lock is where you are opening the file
and writing to it. Since that is not thread safe.
And this will fail if you try to open/write into file from multiple threads.
George
"Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
news:B6D5463F-61F6-41BB-B39D-7679E2587C0A@.microsoft.com...
> Here's an abbreviated version of the full class.
> public class CLog {
> private static string FsLog_File_Path = "";
> private static object FoLockObject = new object();
> private static void RefreshSettings() {
> FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
> lock (FoLockObject) {
> FsLog_File_Path =
> Path.Combine(Path.GetDirectoryName(FsLog_File_Path),
> Path. GetFileNameWithoutExtension(FsLog_File_P
ath) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(FsLog_File_Path));
> }
> }
> public static void WriteInfo(string sDescription) {
> RefreshSettings();
> StreamWriter oStream = null;
> try {
> if (File.Exists(FsLog_File_Path)) {
> oStream = File.AppendText(FsLog_File_Path);
> }
> else {
> oStream = File.CreateText(FsLog_File_Path);
> }
> oStream.AutoFlush = true;
> string sSession = "";
> if (HttpContext.Current != null) sSession =
> HttpContext.Current.Session.SessionID + " - ";
> else sSession = "";
>
> oStream.WriteLine(string.Format("{0} - {1} - {2}{3}",
> DateTime.Now.ToString("dd MMM yyy H:mm:ss"), "INFO", sSession,
> sDescription));
> }
> catch (IOException) {
> }
> finally {
> if (oStream != null) oStream.Close();
> oStream = null;
> }
> }
> }
> "George Ter-Saakov" wrote:
>
Hi George,
I see the error of my ways and have changed it as you suggested. This was
basically a quick hack to include the date in the filename, as this wasn't
the origianl intention.
Thanks for the advice
Ben
"George Ter-Saakov" <gt-nsp@.cardone.com> wrote in message
news:OOv8TCwMGHA.3732@.TK2MSFTNGP10.phx.gbl...
> You do have a problem in your code.
> This line is not thread safe.
> FsLog_File_Path = ConfigurationSettings.AppSettings["logfilepath"];
> You are modifying the variable while some other thread could have been
> doing the code that is in lock {} section.
> So move that line inside (but read further)
> ---
> I am not sure why your are doing it this way. Because I do not see any
> gain in FsLog_File_Path beign global/member variable.
> I would rewrite the code to avoid any synchronization
> private static void RefreshSettings() {
> string sPath =
> ConfigurationSettings.AppSettings["logfilepath"];
> sPath = Path.Combine(Path.GetDirectoryName(sPath),
> Path.GetFileNameWithoutExtension(sPath) + "_" +
> DateTime.Now.ToString("yyyyMMdd") +
> Path.GetExtension(sPath));
> }
> }
>
> As you can see all manipulations are made to local variable so you do not
> need lock.
> ----
> The only place where you will need to lock is where you are opening the
> file and writing to it. Since that is not thread safe.
> And this will fail if you try to open/write into file from multiple
> threads.
> George
>
> "Ben Fidge" <BenFidge@.discussions.microsoft.com> wrote in message
> news:B6D5463F-61F6-41BB-B39D-7679E2587C0A@.microsoft.com...
>