On this page

Another bug bites the dust - complete guide to a road of happiness
.Net framework - open source
Web services - The underlying connection was closed
A new home @ 888.com
Virtual functions under the hood
Can you spot the not ?
A new Phase in life.
Working with the sql server 2000 sequences in c#
Sequence implementation in SQL server 2000
Why did SQL Server 2000 stopped connecting remotely ?

Ads

Navigation

Search

Categories

Clouds

Sql Server (5) .Net (16) .Net 2.0 (2) C# (3) @ff Topic (5) Architectural solutions (9) ASP (1) BDD (5) Blog related (8) database (2) Development process (8) Facebook (1) job interviews (1) Lessons (5) Life (12) Microsoft (5) IIS 6 (2) SPS (sharepoint server) (3) Drivers (1) Internet Explorer (2) Windows 2003 server (1) NightDuck (2) Performance (5) Security (9) Sql Server 2000 (4) Study (2) TDD (1) Threading (3) Under the hood (1) Web (1) Web services (1) XSS (6)

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 63
This Year: 0
This Month: 0
This Week: 0
Comments: 33

Sign In
Pick a theme:

 Saturday, October 13, 2007
Saturday, October 13, 2007 12:29:56 PM (GMT Standard Time, UTC+00:00) ( Lessons | Development process )

I'm sure that if you reading this blog, you have encountered a bug or two (at least).
Now, as you know, often you have to solve bugs of other people that have been fired/left/not available at the moment or just frustrated from it and need a fresh pair of eyes to look at it.

So, you need to deal with the bug, just you and him, like a dual,
What do you do? , how do you confront this mysterious daemon?
 
Most of the people that inherit a bug, clueless regarding his data, try to confront with it straight forward.
Ask a common developer how would he handle a bug, 9 of 10 answers will be "debug".
well, i think it's wrong, it's like knowing that there is a giant brick wall and still running at full speed right in to it.
(unless you are "The Hulk", it won't work)

here is how I'm dealing with a bug :

  • Bring it to your own game court - whenever you have a situation, it's always happens in some environment.
    it's crucial to know it as it was your own place.
    suppose you have a .Net Web application running on a windows server 2003, you need to know damn sure all the environment laws regarding it.
    thing like how IIS handle requests or how security configurations should be, are vital to your diagnosis of the problem.
  • Understand the symptoms right - i have a TV series that i watch , called "House", it's about a brilliant diagnostician doctor that works mostly in unconventional ways and solves the most bizarre cases by doing that.
    i tend to refer this TV series as an educational show on how to handle strange cases and how to approach them.
    on one of it's episodes they had a brilliant dialog:
    - "House, the patients kidneys are shutting down"
    - "Great, we have another symptom"
  • Write it down - get yourself a notebook or even a giant white-board to write all these symptoms that you have found, it should be right in-front of you all the time until you have solved the case.
  • Ask yourself What can cause it ? - preferably do this out loud, ask your self that question regarding each symptom that you found, maybe even with a team , that could confirm it or deny your theories.
  • What was changed ? - this is the most important question that you need to ask yourself.
    things does not go wild on their own,if it worked before and now does not, then something is changed.
    this would be our biggest clue to solve the problem !
    this could be an environmental change, a change in the situation that the application handles or a coding change,
    each one of these can cause a problem, we need to understand which exactly is it.
  • Develop a theory - this is the fun part, now you have all the data that you need to solve this bug.
    you know how the environment behaves, you have the symptoms to the problem, things that was been changed, and possible causes to our symptoms.
    now you need the sit a few minutes and think what is the reason that fits to all our gathered data.
  • Blind tests are the devil - most of them will give you nothing and consume much of your precious time, Get a theory that you think that fits the symptoms and then test it, not the other way around.
  • Develop a solution to the problem - after we have our theory and we tested it, we need to develop a solution that fixes the problem, this is the easy part.

for example, here is a problem that i have encountered almost a year ago:

8007000e System resource exceeded
Microsoft OLE DB Provider for ODBC Drivers error '8007000e'
[Microsoft][ODBC Microsoft Access Driver] System resource exceeded.

lets try to follow these guidelines and solve the problem:

Bring it to your own game court - this is a windows environment that runs an asp web application based on Microsoft access.
Understand the right symptoms - we have a database that denies any further connections due to resource limits.
Write it down - take a moment to look at those symptoms.
Ask yourself what can cause it - it could be an environmental cause, like connection limits are not enough, or coding mistake that suffers from not closing an opened connection.
What was changed ?  - code wasn't changed, all system configurations was not changed, traffic to your site is doubled.
Develop a theory - since the traffic was doubled, the connections are doubled as well, and thus reaching to the connection limit of our application.
why the application does not clearing the connection resources ? - because the code that opens them does not close them, thus the connections piled up and reaching the connection limit way before the recycling process started.
Test it - recycle the connection pool, simulate traffic that won't be in the connections limit(simultaneously), but can be piled up to reach the limit.
Develop a solution to the problem - if you are intrested for this solution , you can check out my post about it

Comments [0] | | # 
 Saturday, October 06, 2007
Saturday, October 06, 2007 9:21:42 PM (GMT Standard Time, UTC+00:00) ( .Net | Microsoft )

Microsoft is going to publish it's source of .Net framework under a referance licience ,
According to this post of scott Gu , i will be intergated in the new Visual studio 2008.

check out this Podcast that Scott Hanselman and Shawn Burke recently recorded.
Not sure what this move will cause, but it's defenatly sounds cool.

maybe it will kill the Mono-project, or maybe give him some strong boost, time will show...

Comments [0] | | # 
 Sunday, September 30, 2007
Sunday, September 30, 2007 2:30:05 PM (GMT Standard Time, UTC+00:00) ( .Net 2.0 | C# | Web services )

recently i encountered some errors with web services regarding webService response.

the full Exception that was thown was :
"Server was unable to process request. -> the underlying connection was closed: A connection that was expected to be kept alive was closed by the server"

hmm, not a very nice exception.
digging a little in , i found few reasons that may cause that error.

  • idle time on the client side is greater than on the server side.
  • network issues that prevent the Keep-Alive feature from being committed. (firewalls, proxies and so...).

what is this KeepAlive feature :
this "great" feature actually keeps the connection between the WebService client and server open for a defined amount of time,
thus enhancing WebService interaction performance by eliminating the need to open a new connection each time.

unfortunately, the time for that window is not synchronized between the client and the server.
this situation creates a problem, on which the client may think that the connection is still open, but the server closed it.

so, what can be done so solve that matter ?

  • ensure the reasons above would not happen
  • disable the KeepAlive feature.

since, ensuring that our time is not greater than the one on the server side is impossible when dealing with 3rd party services,
not even mentioning the 3rd party network configuration, the optimal solution for this issue will be disabling the Keep-Alive feature.

how can we do that ?

when adding a web reference with visual studio 2005, it automatically generates a file called reference.cs which is used as a proxy to the web service on the server.
this is the place which we should do the "fix".

there are 2 ways to prevent this certain problem from happening :

since our "proxy" is being generated each time we update our web reference, we would not want the change to be made in the reference.cs file, since it can be overridden the next time we will do "update web reference"

what we can do, is use the "partial class" feature that presented in .Net 2.0 framework to solve this issue.

namespace theNamespaceOfYourServiceAsStatedInTheReferenceFile
{
public partial class MyService
{
/// <summary>
/// this method overides the base getWebRequest,
/// thus preventing the KeepAlive feature
/// </summary>
/// <param name="uri">the given uri</param>
/// <returns>the needed web request with the disabled keep alive feature</returns>
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
// Do the base class operation and obtain the specific web request
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);

// do the keep alive handling
return Utils.DisableKeepAlive(webRequest);
}
}

now we need to decide how we are going the disable it(write the DisableKeepAlive in our utils)

/// <summary>
/// This method should disable the keep alive feature
/// </summary>
/// <param name="w">the needed HttpWebRequest</param>
/// <returns>Disabled HttpWebRequest</returns>
public static HttpWebRequest DisableKeepAlive(HttpWebRequest w)
{
// Set the keep-Alive to false, thus for not using it
w.KeepAlive = false;


// the second way
w.ProtocolVersion = System.Net.HttpVersion.Version10;

return w;
}

thats it, now we have successfully disabled the KeepAlive feature

Comments [0] | | # 
 Saturday, September 29, 2007
Saturday, September 29, 2007 6:43:04 PM (GMT Standard Time, UTC+00:00) ( Life )

well,
I've been working here for a month now, so it's about time to write a post about it.

888.com A.K.A "random logic" is a ".com" company that started it's way back in 1997.
the company business is online gaming such as casino-online, poker, backgammon and so.
888_logo.gif

I've joined their development department as a part of the "application infrastructure" team.
my initial impression (working a month there...) is that it's a great company working for.
something like google style company, things like : indoor Jim under construction, Nintendo wii/Sony play-station 3 on each floor, personal medical insurance, cool trips and so ...

on the professional side, I've joined a team which i have things to learn from, both technical and architectural.
thus contributing my knowledge and experience.
I'm sure that will be a great place to evolve to the next level (Pokémon ?).

wish me luck .

Comments [1] | | # 
 Saturday, July 21, 2007
Saturday, July 21, 2007 1:39:10 PM (GMT Standard Time, UTC+00:00) ( .Net | job interviews | Under the hood )

a little background to that question,
in the last week I'm doing some sort of a job interviews marathon, being asked for some quite interesting questions (most of them are really simply OPP questions).
but this one caught me off guard, wouldn't expect this one as a pre-interview question over the phone.

so, how does this strange creature work (which most of us using it without even knowing what it does behind) ?

in contrast to regular method calling, the virtual method calling isn't called directly, instead it uses the "Virtual Functions Table". (C# compiler implements it with VFT, other compilers may implement it via binary trees)
in short, it uses pointers to functions table, to map the call to the right method.
lets take a look at an example :

public class Father
{
public virtual void foo()
{
Console.Write("string from the father");
}

public virtual void foo2()
{
Console.Write("foo2 string from the father");
}

internal void boo()
{
Console.Write("father says boo");
}
}

public class Son : Father
{
public override void foo()
{
Console.Write("string from the son");
}
public void moo()
{
Console.Write("son says moo");
}
public void faaBase()
{
base.foo();
}
}

we have 2 classes ,son derives from the father, and overrides one of his virtual methods.

Father f = new Father();
Son s = new Son();

// Father calls
// Virtual method call
f.foo();
f.foo2();

// Non virtual method calls
f.boo();

// Son calls
// Virtual method call
s.foo();

// Non virtual method calls
s.boo();
s.moo();

what really happens behind ?
lets view a part of the disassembly code

// Virtual method call
s.foo();
00000065 mov ecx,esi
00000067 mov eax,dword ptr [ecx]
00000069 call dword ptr [eax+38h]
0000006c nop

s.foo2();
0000006d mov ecx,esi
0000006f mov eax,dword ptr [ecx]
00000071 call dword ptr [eax+3Ch]
00000074 nop

// Non virtual method calls
s.boo();
00000075 mov ecx,esi
00000077 cmp dword ptr [ecx],ecx
00000079 call FFAB30A0
0000007e nop

s.moo();
0000007f mov ecx,esi
00000081 cmp dword ptr [ecx],ecx
00000083 call FFAB3158
00000088 nop


we can see clearly that the non virtual call has a direct calling to the function (a hard coded address),
whereas the virtual method calling points to the virtual method table that resides in the Son object.
lets take a look on that table :

EEClass: 00a21370
Module: 00a22c24
Name: VTF.Son
mdToken: 02000004 (E:\PROJECTS\vtf 2005\VTF\VTF\bin\Debug\VTF.exe)
BaseSize: 0xc
ComponentSize: 0x0
Number of IFaces in IFaceMap: 0
Slots in VTable: 9
--------------------------------------
MethodDesc Table
Entry    MethodDesc JIT     Name
7934cdcc 79137ab8    PreJIT System.Object.ToString()
7934bba0 79137ac0    PreJIT System.Object.Equals(System.Object)
7934bb90 79137ad8    PreJIT System.Object.GetHashCode()
793424c0 79137ae0    PreJIT System.Object.Finalize()
00a231b8 00a23140    JIT    VTF.Son.foo()
00a23100 00a23088    JIT    VTF.Father.foo2()

00a231c8 00a23148    NONE   VTF.Son.moo()
00a231d8 00a23150    NONE   VTF.Son.faaBase()
00a231e8 00a23158    JIT    VTF.Son..ctor()

 

we can see a couple of things from this table:

  • VT lists all the methods the son object holds
  • VT lists the virtual functions of the father that are virtual
  • When the son object overrides one of the virtual methods that the father implements,
    the father method entry is being replaced by the new son method (line 5 at the table - Son.foo())
  • the VT does not list father method that are not virtual.

actually the Son.moo() method looks a little unnecessary in the VT due to the fact that the function is not virtual and will be addressed directly and not by the VT.

in conclusion, think twice before you declare a method as virtual, because it contains some performance hit

Comments [4] | | # 
Friday, July 20, 2007 11:26:24 PM (GMT Standard Time, UTC+00:00) ( Security )

intresting security test to spot the scam sites.
got an 9/10 , the amazon question got me ....

can you spot the not ?
http://www.siteadvisor.com/quizzes/phishing_0707/

 

Comments [0] | | # 
 Friday, July 20, 2007
Friday, July 20, 2007 1:57:30 PM (GMT Standard Time, UTC+00:00) ( Life )

After Three years, i have left my workplace where i started  as a new programmer, and after a while became a senior developer.
got to say, it was a lot of fun working with some gifted developers like Shani , Avi , Nati , Doron , Oren , Yossi
thus not forget my teammates : Gabi, Assaf, Tanya, Amihay (Please create a blog somewhere, it's about time...)
and many more (I'm sure I'm forgetting someone, apologies in advance...)

I must say that i gained tons of experience in Web development, Server side engines, project management, Requirement Analyzing, Enterprise development,Software design , Database development and many other issues in software development.

wish you luck and all the best for those who i "left behind".

what is next ?
thats a good question, planning to finish my Bsc degree in computer science (third and last year).
and getting a job that won't mess up my studies :)
So basically I'm in search for the right job.

Comments [2] | | # 
 Thursday, July 19, 2007
Thursday, July 19, 2007 8:12:19 PM (GMT Standard Time, UTC+00:00) ( Sql Server  | C# | Sql Server 2000 )

For those who didn't read the previews post about Sequence Implementation In SQL Server 2000 , please read it before continuing.

basically the idea is quite simple but i tough to add this post to complete the sequence framework.
adding some handling functions to your DAL object :

        /// <summary>
        /// this procedure retrieves from the database the value of the desired sequence.
        /// this is a generic function that gets the stored procedure to get the data from and the sequence name itself
        /// </summary>
        /// <param name="seqName">the name of the desired sequence</param>
        /// <param name="storedProcedure">the stored prosedure to call</param>
        /// <param name="connection">the connection to the database</param>
        /// <returns>an integer that represent the value of the sequence</returns>
        internal virtual int GetSequenceID(string seqName,string storedProcedure, SqlConnection connection)
        {
            int ret = int.MinValue;
            
            // Create a command
            SqlCommand command = new SqlCommand(storedProcedure, connection);    
            command.CommandType = CommandType.StoredProcedure;

            // pass the parameter of the sequence name for the series sequence
            SqlParameter p1 = new SqlParameter("@seqName",SqlDbType.VarChar);
            p1.Value = seqName;
            p1.Direction = ParameterDirection.Input;
            command.Parameters.Add(p1);        

            // open connection or using it if already opened
            bool OpenedConnection = false;
            try
            {
                if(connection.State!=System.Data.ConnectionState.Open)
                {
                    OpenedConnection = true;
                    connection.Open();
                }            
                
                // Execute the command
                ret = Convert.ToInt32(command.ExecuteScalar());
            }
            finally
            {
                if ((OpenedConnection) && (connection.State != System.Data.ConnectionState.Closed))
                    connection.Close();
            }
            
            return ret;
        }


        /// <summary>
        /// this is an internal function that retrieves the sequence value
        /// generally, it wraps the other internal method that does the actual work
        /// </summary>
        /// <param name="seqName">the name of the sequence that we want</param>
        /// <param name="storedProcedure">the stored procedure that we address for</param>
        /// <returns>the value of the sequence</returns>
        internal int GetSequenceID(string seqName,string storedProcedure)
        {
            int ret = int.MinValue;

            // Use the connection
            using(SqlConnection connection = DalServices.Connection())
            {
                try
                {
                    // Open the connection
                    connection.Open();
                    
                    // Get the object throught the internal method
                    ret = GetSequenceID(seqName,storedProcedure,connection);
                }
                
                finally
                {
                    // Close the connection
                    if (connection.State != System.Data.ConnectionState.Closed) connection.Close();
                }
            }
            
            return ret;
        }


and add some wrapping functions to extern the methods:

        /// <summary>
        /// The Next value of the series sequence
        /// </summary>
        /// <returns>the value of the sequence</returns>
        public int GetNextSeries()
        {
            // Calling the internall function to get the value
            return GetSequenceID("sq_series","sp_SequenceNextVal");
        }

        /// <summary>
        /// The current value of the series sequence
        /// </summary>
        /// <returns>the value of the sequence</returns>
        public int GetCurrentSeries()
        {
            // Calling the internall function to get the value
            return GetSequenceID("sq_series","sp_SequenceCurrVal");
        }

once again i'm a happy kamper.
Comments [0] | | # 
 Wednesday, July 18, 2007
Wednesday, July 18, 2007 7:06:10 PM (GMT Standard Time, UTC+00:00) ( Sql Server  | Architectural solutions | Sql Server 2000 )

for those folks who worked with oracle, know that sequence is an internal database object which has it's own methods and properties.
hey, why shouldn't i use this wonderful auto number feature that sql server has ?
well, auto number will give you a good solution to most of your needs that a sequence provided in oracle.
but, here is some conditions where auto number just won't suffice :

  • Managing more than one "auto number" in one table - auto number can be defined only at one field.
  • Managing the same auto number across multiple tables - the auto number can be asigned only to a specified table.

the solution is simple:
we will create some framework that will allow us to manage sequences and work with them.

first we will create one table to contain the sequences.

CREATE TABLE [dbo].[sysSequences] (
    [SequenceName] [char] (30) COLLATE Hebrew_CI_AS NOT NULL ,
    [Incrasement] [int] NOT NULL ,
    [StartValue] [int] NOT NULL ,
    [CurrentValue] [int] NOT NULL
) ON [PRIMARY]
GO

next thing we need to do is create some stored procedures that will handle these sequences.

the create sequence procedure :

CREATE procedure dbo.sp_Create_Sequence
(
    @seqName     varchar(30),
    @Increasement     int,
    @startValue    int
)
as
begin

declare @r int
begin transaction

    -- Check if the sequence exists
    set @r =(    select count(*)
    from sysSequences where SequenceName = @SeqName)
    
    if @r <> 0 --if error is raised
    begin
        goto LogError
    end

insert into sysSequences (SequenceName,Incrasement,StartValue,CurrentValue)
values (@seqName,@Increasement,@startValue,@startValue)    

commit transaction
goto ProcEnd

LogError:
rollback transaction
RAISERROR ('Can not create sequence with name %s, sequence exists already.',16,1,@seqName)

ProcEnd:
end

once we created the "object" (not really an object, more as a row in the sequence table), lets get his current value

create procedure dbo.sp_SequenceCurrVal
(
    @seqName     varchar(30)

)
as
begin

declare @rows int

begin transaction
    -- Check if the sequence exists
    set @rows =(    select count(*)
    from sysSequences where SequenceName = @SeqName)
    
    -- if no such row
    if @rows = 0
    begin
        goto LogError
    end

    -- Get the new value
    select CurrentValue from sysSequences
    where SequenceName = @SeqName
    
commit transaction
goto ProcEnd

LogError:
rollback transaction
RAISERROR ('sequence named %s, does not exists.',16,1,@seqName)

ProcEnd:
end

and the "get next value" method (very similar to the get current procedure.)

create procedure dbo.sp_SequenceNextVal
(
    @seqName     varchar(30)

)
as
begin

declare @rows int

begin transaction
    -- Check if the sequence exists
    set @rows =(    select count(*)
    from sysSequences where SequenceName = @SeqName)
    
    -- if no such row
    if @rows = 0
    begin
        goto LogError
    end


    -- Update the sequence increasement
    update sysSequences
    SET CurrentValue = CurrentValue + Incrasement
    where SequenceName = @SeqName

    -- Get the new value
    select CurrentValue from sysSequences
    where SequenceName = @SeqName
    
commit transaction
goto ProcEnd

LogError:
rollback transaction
RAISERROR ('sequence named %s, does not exists.',16,1,@seqName)

ProcEnd:
end


thats it, done.
now we have some sql server sequences

 

Comments [0] | | # 
 Saturday, July 14, 2007
Saturday, July 14, 2007 7:59:46 AM (GMT Standard Time, UTC+00:00) ( Windows 2003 server | NightDuck | Sql Server 2000 )

or, What have the latest windows 2003 service pack 2 has done to my sql server ?


Today i scheduled for my self some working time on the NightDuck project (Coming Soon),
but suddenly something went wrong, the sql server, which installed on a physically different machine than the development rig, gave me the following message:

"Sql server does not exist or access denied ConnectionOpen(Connect())"

what could have go wrong ?

the interesting part is when i tried to connect to the server remotely (remote desktop) and connecting to the Sql server from the localhost, it had no problems what so ever.
Since the sql server machine is not a dependency of a production application or something like that, i went wild on the experiments to try and fix the problem

  • Lets restart the sql server service - no good.
  • Router configurations and port forwarding - nothing has changed since the last time i worked on the project, thats not the problem either
  • SQL SERVER user - defining a new user did not solved the problem either

so i started googling on the subject, and found a few things, like this one for example at the Microsoft site :
Potential causes of the "SQL Server does not exist or access denied" error message

this article has a list of things that may cause this problem (hard to guess by it's name).
checking the relevant ones, i came across this article about ports needed by the sql server.
i made a little test to check if the ports are ok, i tried to telnet the machine from the dev PC on the 1433 port, but with no luck,
in an effort to isolate the problem i tried that from the localhost of the server, telneting to the localhost on port 1433, which gave me an error "could not connect...".
this is what i was looking for !
At this point the problem is completely isolated, why ?

  • the router configuration is not an issue (request does not go out from the machine)
  • the development rig is not part of the equation anymore
  • firewall is not the problem

based on these symptoms, I've searched once more, and stumbled upon a forum thread about a very similar issue, when installing service pack 2 on windows XP causes the same effect.
Apparently, if you have an out of date version of the file DBNETLIB.dll ,the Service Pack 2 disables TCP/IP access to MS SQL Server.
This is a defense against the Slammer worm. (some ugly work around by Microsoft a have to say...)

the solution is installing the SQL Server 200 security tools , which sets things back to normal.

since the windows 2003 service pack 2 is quite new, i guess this thread will be useful


update :
Just wondering how many others encountered this specific problem,
if you find this info useful, please leave a comment.

Comments [1] | | #