Tuesday, January 18, 2011

Web Farm and Web Garden

Web garden in asp.net :
When an application is hosted by multiple processes on the same server it is said to be a web garden environment. An application pool with multiple worker process is called Web Garden, means single server and multiple CPUs.

All IIS Request process by worker process ( w3wp.exe), In web garden scenario If we use Session Mode to "in proc", our application will not work correctly because session will be handled by different Worker Process. For Avoid this Type of problem we should have to use Session Mode "out proc" and we can use "Session State Server" or "SQL-Server Session State".


Web farm means running one asp.net application on multiple machine.

Events in HTTPApplication in Asp.net

Here are the event occurred in life cycle of HTTPApplication

1. BeginRequest. The request processing starts.
2. AuthenticateRequest. The request is authenticated. IIS 7 and ASP.NET authentication modules subscribe to this stage to perform authentication.
3. PostAuthenticateRequest.
4. AuthorizeRequest. The request is authorized. IIS 7 and ASP.NET authorization modules check whether the authenticated user has access to the resource requested.
5. PostAuthorizeRequest.
6. ResolveRequestCache. Cache modules check whether the response to this request exists in the cache, and return it instead of proceeding with the rest of the execution path. Both ASP.NET Output Cache and IIS 7 Output Cache features execute.
7. PostResolveRequestCache.
8. MapRequestHandler. This stage is internal in ASP.NET and is used to determine the request handler.
9. PostMapRequestHandler.
10. AcquireRequestState. The state necessary for the request execution is retrieved. ASP.NET Session State and Profile modules obtain their data.
11. PostAcquireRequestState.
12. PreExecuteRequestHandler. Any tasks before the execution of the handler are performed.
13. ExecuteRequestHandler. The request handler executes. ASPX pages, ASP pages, CGI programs, and static files are served.
14. PostExecuteRequestHandler
15. ReleaseRequestState. The request state changes are saved, and the state is cleaned up here. ASP.NET Session State and Profile modules use this stage for cleanup.
16. PostReleaseRequestState.
17. UpdateRequestCache. The response is stored in the cache for future use. The ASP.NET Output Cache and IIS 7 Output Cache modules execute to save the response to their caches.
18. PostUpdateRequestCache.
19. LogRequest. This stage logs the results of the request, and is guaranteed to execute even if errors occur.
20. PostLogRequest.
21. EndRequest. This stage performs any final request cleanup, and is guaranteed to execute even if errors occur.

By using the familiar ASP.NET APIs, the ability to execute in the same stages as IIS 7 modules makes tasks that were only previously accessible in native ISAPI filters and extensions now possible in managed code.

Friday, January 7, 2011

Update records row by row using cursor in sql

How to loop through a table and update row by row using cursor in sql. ...basically looping through a table and updating record one by one in ms sql.

declare @cur1 cursor
declare @col1 varchar(10)
declare @col2 varchar(10)
declare @col3 varchar(10)
declare @col4 varchar(10)

set @cur1 = Cursor FOR SELECT column4,column1,column3 from myTable1

OPEN @cur1
FETCH NEXT FROM @cur1 into @col4, @col1, @col3
While @@FETCH_STATUS = 0
begin
select @col2=column2 from differentTable2 where someColumn=@col3

if @col2 is not null
begin
Update myTable1
set column2 = @col2
Where column4=@col4
end

FETCH NEXT FROM @cur1 into @col4, @col1, @col3
End
close cur1
deallocate cur1

Wednesday, January 5, 2011

Nhibernate Session Module Design

Session Module is the heart & head of nhibernate framework, so here i am publishing the design of SessionModule

Reference required :
using System;
using System.Web;
using NHibernate;
using NHibernate.Cfg;

Session Module class :
public class SessionModule : System.Web.IHttpModule
{
private static readonly string SESSION_KEY = "CONTEXT_SESSION";
private const string TRANSACTION_KEY = "CONTEXT_TRANSACTION";

[ThreadStatic]
private static NHibernate.ISession m_session;

private static ISessionFactory m_sessionFactory;

public void Dispose()
{

}

public void Init(System.Web.HttpApplication context)
{
log4net.Config.XmlConfigurator.Configure();
context.BeginRequest += new EventHandler(BeginRequestHandlerExecute);
context.EndRequest += new EventHandler(EndRequestHandlerExecute);
// http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx
Configuration cfg = new Configuration();
m_sessionFactory = cfg.BuildSessionFactory();

}

private void BeginRequestHandlerExecute(object sender, EventArgs e)
{
CurrentSession = m_sessionFactory.OpenSession() ;

}

private void EndRequestHandlerExecute(object sender, EventArgs e)
{
ISession currentSession = CurrentSession;
if (currentSession != null)
currentSession.Close();
}

public static ISession CurrentSession
{
get
{
ISession session = (ISession)HttpContext.Current.Items[SESSION_KEY];
return session;
}
set
{
HttpContext.Current.Items[SESSION_KEY] = value;
//SetCurrentSession(value);
}
}


public static ITransaction CurrentTransaction
{
get
{
ITransaction tran = (ITransaction)HttpContext.Current.Items[TRANSACTION_KEY];
return tran;
}
set
{
HttpContext.Current.Items[TRANSACTION_KEY] = value;
}
}
}

Monday, January 3, 2011

Nhibernate Quick Overview

Download all required nhibernate from following url.

http://sourceforge.net/projects/nhibernate/


How to use ICriteria ?

ICriteria criteria = SessionManager.CurrentSession.CreateCriteria();
criteria = criteria.Add(NHibernate.Criterion.Expression.Eq("StudentId", 48));
Student stu= criteria.UniqueResult();

How to Map two or more database table from one object in nhibernate ?

Suppose thhe following school object does not have a property called Student ,

public class School
{
public virtual ISet Students { get; set; }
}


Now in hbm you can map like

< set name="Students" table="Students" schema="oneSchool" cascade="all-delete-orphan" lazy="true" where ="AdminDate() between StartDate And IsNull(EndDate,GetDate())">
< key column="SchooldId" / >
< many-to-many class="Student" column="StudentId"/ >
< /set>

how to use transaction in Nhibernate ?

use Nhibernate.Transaction namespace reference;
using (ITransaction tran = SessionManager.CurrentSession.BeginTransaction())
{
tran.commit();
}