dear Gert,
i''m using a dbManager class which loads the configuration and i'm opening 1 session abd connecting and disconnecting to that session .
does that affect the nhibernate lazy loading ?
p.s:i'm using session.connect() and session.disconnect() in order to minimize the access to the database, is this true ?
or is the connection to the database opened in both cases ( if session is connected or not) ?
how can i optimize .
This is some of the code :
Code:
static Configuration config;
static ISessionFactory factory;
static ISession session;
static DBHandler()
{
config = new Configuration();
config.AddClass(typeof(DataTutorials.BLL.Department));
//config.AddClass(typeof(DataTutorials.BLL.Student));
//config.AddClass(typeof(DataTutorials.BLL.Professor));
config.AddClass(typeof(DataTutorials.BLL.Person));
config.AddClass(typeof(DataTutorials.BLL.UniversityClass));
try
{
factory = config.BuildSessionFactory();
session = factory.OpenSession();
session.Disconnect();
}
catch (Exception ex)
{
string errormsg = ex.Message;
throw new ArgumentOutOfRangeException("DBHandler - ERROR IN THE CONSTRUCTOR ", ex, ex.Message);
}
}
public IList GetDepartments()
{
session.Reconnect();
ITransaction tx= session.BeginTransaction();
IList retrievedDepts=null;
try
{
retrievedDepts = session.CreateCriteria(typeof(DataTutorials.BLL.Department)).List();
}
catch (Exception ex)
{
tx.Rollback();
string errormsg = ex.Message;
throw new ArgumentOutOfRangeException("DBHandler - ERROR IN getDepartments ", ex, ex.Message);
}
session.Disconnect();
return retrievedDepts;
}
public Department GetDepartment(int deptID)
{
session.Reconnect();
ITransaction tx = session.BeginTransaction();
Department retrievedDept = null;
try
{
retrievedDept = (Department)session.Load(typeof(DataTutorials.BLL.Department), deptID);
}
catch (Exception ex)
{
tx.Rollback();
throw new ArgumentOutOfRangeException("DBHandler - ERROR IN Getting Department ", ex, ex.Message);
}
session.Disconnect();
return retrievedDept;
}
public void SaveDepartment(Department dept)
{
session.Reconnect();
ITransaction tx = session.BeginTransaction();
try
{
session.SaveOrUpdate(dept);
tx.Commit();
}
catch(Exception ex)
{
tx.Rollback();
throw new ArgumentOutOfRangeException("DBHandler - ERROR IN Save Department ", ex, ex.Message);
}
session.Disconnect();
}
public void DeleteDepartment(Department dept)
{
session.Reconnect();
ITransaction tx = session.BeginTransaction();
try
{
//session.deleteItem(Item);
session.Delete(dept);
tx.Commit();
}
catch (Exception ex)
{
tx.Rollback();
throw new ArgumentOutOfRangeException(ex.Message, ex, ex.Message);
}
session.Disconnect();
}
i would be glad to hear ur comments and suggestions .
Regards,
Jojo