Jun112008

Connection pool is full

Published by volkanuzun at 3:22 PM under

Today, we got a call from one of students, complaining that the site doesnt work, so i tried to load the page myself, nope, it didnt work. I connected to the server using remote desktop connection, and everything was working fine. At first i really didnt understand the problem, so i opened the event viewer to see the event logs. The first thing i saw is a warning message saying "Connection pool is full, and can not connect to pool..." , as someone who is using google more than anything else during a day, i opened my browser, and typed the warning into the google. The sites i have found was talking about if you open a connection to the database, and before you close the connection an exception is thrown and u dont use try catch, u have a wasted connection now. And you have too many wasted connection, the pool will be full and you cant make another connection. so watch this :

private bool DoSomethingWithMyDatabase()
{
     SqlConnection conn = new SqlConnection...
     ....
     .....
    conn.Dispose();

 

if you have a code like above, and an exception is thrown between new SqlConnection and conn.Dispose, you dispose function is never called, and you have one wasted connection now, and if this happens too much in a short time, your pool is going to be full, and your application will stop working.  Ok now how do fix it ?

The first dirty patch, is opening up iis manager, and restarting the web application pool and waiting for the next connection full error.

The real patch is, changing ur code, and putting try catch blocks or using block such as:

 private bool DoSomethingWithMyDatabase()
{
     SqlConnection conn = new SqlConnection...
     try{
      ....
     .....
     }
     finally{
    conn.Dispose();
    }

 

so even if an exception is thrown, use can safely dispose ur connection, and ur connection will be returned back to the pool. Another solution is : 

 

 private bool DoSomethingWithMyDatabase()
{
   using(SqlConnection conn = new SqlConnection())
    {

     }

 

 

[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Jun042008

System.Web.Caching.Cache and error on my page

Published by volkanuzun at 11:23 AM under

I am working on this web site, that does a lot of database queries, so i decided to use some caching mechanism here.

I do have a static function which reads the database, create a collection out the result and returns it to the caller, below is the simple code :

  private static List<WebSite> GetWebSites()
    {
        List<WebSite> WebSites = (List<WebSite>)Cache["WebSites"];
        if (WebSites == null)
        {
            DatabaseAccess db = new DatabaseAccess(ConfigurationManager.ConnectionStrings["CSUWebSitesConnectionString"].ConnectionString);
            WebSitesTB webSitesTB = new WebSitesTB(db);
            WebSites = webSitesTB.GetAllWebSites();
            Cache["WebSites"] = WebSites;
        }
     ......

but i got the error:  Error    39    An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Cache.get'    D:\WORK PROJECTS\CSUSBWebSitesApp\WebSitesApp\Administration\WebSites.aspx.cs    43    49    D:\...\WebSitesApp\

Can you guess what the problem is ? 

The problem is: Cache is a property of the class Page, and i am trying to access a property of page class, from a static function ; however this is not possible. What you should do is, change Cache to HttpContext.Current.Cache

 



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Jun032008

Where does linq sit at?

Published by volkanuzun at 9:04 AM under

I am still trying to understand what does Linq sit at?

if i have DAL,BAL, and PL, which layer will have linq quries?

it could be in the dal, and accesses the sql server and returns collection of objects, however

it could also be in bal and query the returned collection using linq

it could be in pl to sort grid etc



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

May222008

coding in my dreams?

Published by volkanuzun at 10:02 PM under

i dont remember my dreams; no seriously i dont. I cant really say i dont dream while i am asleep, cause i dont remember if any :)

So, my wife; a few times told me that i talked to her when i was sleeping ;) probably i was dreaming. She told me yesterday i was telling her; "these benchmarks are very important for performance".  I have no idea what those benchmarks were, but it was a proof for me that i dream :), interesting that i dream about coding ; a few todays ago, my wife this time told me , i was talking about some algorithm.

I wish i could pop a usb memory in my ear (hey i said ear ok ? ), and record my dreams; that would be fun 



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 1 Responses

May212008

disable debugging in ur server

Published by volkanuzun at 8:36 PM under

Ok we all know we should change debug mode to false in our web config when it is time to upload the site to the server; and guess what, we might forget it right ?

I just learnt the easiest way :) of disabling debug to false in the web server. Go to machine.config file in the web server, find system.web section and add <deployment retail="true"/> 

is that it or what :) 



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

May202008

I passed 70-431

Published by volkanuzun at 11:28 PM under

i was studying for the MS 70-431 Implemantation and Administration Sql Server 2005 exam in the last 4 weeks, besides the projects @work, this kept me really busy. Even though i use sql server 2005 in work daily, you never what might microsoft ask you :), so i bough the ms press book for the exam, and went through each chapter,  and today i passed it :) 

Now as i didnt get the email from microsoft yet, i am not %100 sure about what certifications i will get by passing this exam, but according to microsoft's web site, i am now MCP, and MCTS. my next target is asp.net web development certification track, let see :)

 



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 1 Responses

May182008

XML, Linq, GridView, Label

Published by volkanuzun at 8:59 AM under

I had a very small project, where there would be page that lists events, another page that adds events. I didnt want to have a database for this simple pages, so i decided to go on with xml file. My xml file structure is basically like below: 


Now i need a pagethat will list all the events (hopefully ordering them according to When attribute which is a datetime. so i created a page put a label on the page, and open the code behind file.

What i want to do is, open the xml file, order them, and dump the data into the label, so i decided to use linq, as i am learning this new technique. Below is the simple code for this, linq made it so easy for me to parse the xml; unbelievable:

 


BTW you could do the exact same thing with xmldatasource and xpath, however; i am not sure with ordering :), ordering wont be that easy i guess with xmldatasource, here is the page that does the above listing with xmldatasource and gridview and xpath:

 



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 2 Responses

May012008

Change DB Access to Interface

Published by volkanuzun at 10:03 AM under

As i mentioned in my previous posting, i am trying to seperate the modules in my projects so i can test every single code. I am new to TDD so i meet a lot of challenges during the adoptation. One of them is the database access, how will i unit test a class or a function where these is a database dependency.  i decided to create a generic database access class, which implements an interface that could be mock out.

In the database side, i always use stored procedures to do actions, and my stored procedures either returns an integer value (if it doesnt return anything like delete from..., consider it returns 1),  or a DataTable such as select * from table. So i can easily write an interface such as:


public interface IExecuteProcedure
    {
        DataTable GetDataTable(string StoredProcedureName, List<SqlParameter> Parameters);
        int ExecuteStoreProcedure(string StoredProcedureName, List<SqlParameter> Parameters);
    }

So GetDataTable runs a stored procedure with a list of parameters given , and returns a DataTable, ExecuteStoreProcedure returns int.  A database access class could follow a pattern like below to implement this interface:


public class DatabaseAccess:IExecuteProcedure
    {
        private readonly string ConnectionString_;

       
        public DatabaseAccess(string ConnectionString)
        {
            this.ConnectionString_ = ConnectionString;
        }
       
        public DataTable GetDataTable(string StoredProcedureName, List<SqlParameter> Parameters)
        {
            SqlConnection conn = new SqlConnection(ConnectionString_);
            SqlCommand cmd = new SqlCommand(StoredProcedureName, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter param in Parameters)
            {
                cmd.Parameters.Add(param);
            }
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
            }
            catch (Exception)
            {
                dt.Clear();
            }

            return dt;
        }

        #region IExecuteProcedure Members

        public int ExecuteStoreProcedure(string StoredProcedureName, List<SqlParameter> Parameters)
        {
            SqlConnection conn = new SqlConnection(ConnectionString_);
            SqlCommand cmd = new SqlCommand(StoredProcedureName, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter param in Parameters)
            {
                cmd.Parameters.Add(param);
            }
            int iReturn = 0;
            try
            {
                conn.Open();
                cmd.ExecuteScalar();

                for (int i = 0; i < Parameters.Count; i++)
                {
                    if (Parameters[i].Direction == ParameterDirection.Output)
                    {
                        Parameters[i].Value = cmd.Parameters[Parameters[i].ParameterName].Value;
                        iReturn = Int32.Parse(Parameters[i].Value.ToString());
                    }
                }
            }
            catch(Exception ex)
            {
                iReturn = Int32.MinValue;
            }
            finally
            {
                conn.Dispose();
            }
            return iReturn;
        }

        #endregion

       
    }

So the database access class implements the interface, and returns the appropriate values. Now in my next posting we will see how we can use this class to access the database, but also we will see how we can inject some code, and change the behaviour without changing the structure.

 

 



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Apr302008

TDD, Rhino.Mocks

Published by volkanuzun at 10:22 AM under

I am pretty new to testing your codes using nunit and rhino.mocks. i am having lots of problems and thank to Tom Opgenorth, i solve each of these slowly. From today, i will be sharing the difficulties i have in my TDD world. I am pretty new to testing, so a

To test every single function in your code, the whole programming paradigm has to change. you have to break your code into smallest available pieces that allows to you to write test functions. It seemed to me at the beginning that you lose some performance doing so but with today's hardware, you concentration should be on maintable, working, bug free, tested code. So in the next posting, i will share one of my simple class, and how i used nunit to test simple things.



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Apr212008

Google's inaccurate search results

Published by volkanuzun at 8:57 AM under

I am writing a search page for our campus using google search applicance. Within my application i called a httprequest to google's mini machine, and parse the returning xml file. Depending on the number of results returned from google (it is a parameter in the xml file) i am creating the pagination. However it wasnt working, i was debugging my code, and reading google's documentation. Guess what, google never tells you how many search results there are, it just guesses the number of search result but as soon as you click on other pages, you might get nothing :), and different estimation. I tried this with google, search anything at google, and note down the number of results , and click page 2; the number of results are changed right :)

waoov so they are just guessing baby...



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses