I always don't know how to resolve my matter.
I will give you more details.
When I want to access data, the source code is as following.
Code:
public void doTreatement(object o)
{
prepareSessionX();
doJob(); // ex : session.Delete(o);
unprepareSessionX();
}
To prepare or unprepare the session, I first use the following code because I read that it is better to open, do job and close the session.
Code:
//////////////////////////////////////
// FIRST
// performance : good with SQL Server, very slow with Firebird
// error : none
private void prepareSession1()
{
session = factory.OpenSession();
}
private void unprepareSession1()
{
if (session != null)
session.Close();
}
Because of the very slowness of this code with Firebird, I try to keep the connection as a static member of the class to use only one connection.
Code:
//////////////////////////////////////
// SECOND
// performance : good with SQL Server, very slow with Firebird
// => the session.Close() function close the connection
// So, the connection.Open() is always executed.
// error : none
private void prepareSession2()
{
if (session == null)
{
session = factory.OpenSession();
connection = session.Connection;
}
else
{
if (connection.State != System.Data.ConnectionState.Open)
connection.Open();
session = factory.OpenSession(connection);
}
}
private void unprepareSession2()
{
if (session != null)
session.Close(); // but this close the connection
}
When I remark that the connection is closed when session.Close() is called, I decided not to close the session but only disconnect it.
Code:
//////////////////////////////////////
// THIRD
// performance : good with SQL Server and Firebird
// => fast because the session is always the same
// and keep track of last objects load.
// error : because the session is always the same,
// the session keep track of deleted objects for example
// and an exception is thrown.
private void prepareSession3()
{
if (session == null)
{
session = factory.OpenSession();
}
else
{
session.Reconnect();
}
}
private void unprepareSession3()
{
if (session != null)
{
session.Disconnect(); // but this close the connection
}
}
What is the best way to manage session ?
Thanks.