Thank you kindly, Tlaukkan (Tommil) for a patch that:

NHibernate MySQL migration was not working as mysql dialect is 
MySQL5Dialect now instead of MySQLDialect which is the migration 
sub folder name. Fixed this by adding simple dialect to migration 
sub type mapping to manager initialization to avoid need of renaming 
migration script folder each time MySQL version changes. Removed 
shared session and changed session to be constructed per call as 
NHibernate session is not thread safe. Refactored manager member 
names to be according to the naming convention (full words in camel case).
0.6.2-post-fixes
Charles Krinke 2009-01-10 16:31:00 +00:00
parent 630ce5b264
commit 16561bd528
1 changed files with 73 additions and 45 deletions

View File

@ -42,9 +42,8 @@ namespace OpenSim.Data.NHibernate
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private string dialect; private string dialect;
private Configuration cfg; private Configuration configuration;
private ISessionFactory factory; private ISessionFactory sessionFactory;
private ISession session;
public NHibernateManager(string connect, string store) public NHibernateManager(string connect, string store)
{ {
@ -61,41 +60,53 @@ namespace OpenSim.Data.NHibernate
dialect = parts[0]; dialect = parts[0];
// NHibernate setup // NHibernate setup
cfg = new Configuration(); configuration = new Configuration();
cfg.SetProperty(Environment.ConnectionProvider, configuration.SetProperty(Environment.ConnectionProvider,
"NHibernate.Connection.DriverConnectionProvider"); "NHibernate.Connection.DriverConnectionProvider");
cfg.SetProperty(Environment.Dialect, configuration.SetProperty(Environment.Dialect,
"NHibernate.Dialect." + dialect); "NHibernate.Dialect." + dialect);
cfg.SetProperty(Environment.ConnectionDriver, configuration.SetProperty(Environment.ConnectionDriver,
"NHibernate.Driver." + parts[1]); "NHibernate.Driver." + parts[1]);
cfg.SetProperty(Environment.ConnectionString, parts[2]); configuration.SetProperty(Environment.ConnectionString, parts[2]);
cfg.AddAssembly("OpenSim.Data.NHibernate"); configuration.AddAssembly("OpenSim.Data.NHibernate");
//To create sql file uncomment code below and write the name of the file //To create sql file uncomment code below and write the name of the file
//SchemaExport exp = new SchemaExport(cfg); //SchemaExport exp = new SchemaExport(cfg);
//exp.SetOutputFile("nameofthefile.sql"); //exp.SetOutputFile("nameofthefile.sql");
//exp.Create(false, true); //exp.Create(false, true);
factory = cfg.BuildSessionFactory(); sessionFactory = configuration.BuildSessionFactory();
session = factory.OpenSession();
Assembly assem = GetType().Assembly; Assembly assembly = GetType().Assembly;
Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, store);
m.Update(); // Migration subtype is the folder name under which migrations are stored. For mysql this folder is
// MySQLDialect instead of MySQL5Dialect which is the dialect currently in use. To avoid renaming
// this folder each time the mysql version changes creating simple mapping:
String migrationSubType = dialect;
if (dialect.StartsWith("MySQL"))
{
migrationSubType="MySQLDialect";
}
Migration migration = new Migration((System.Data.Common.DbConnection)sessionFactory.ConnectionProvider.GetConnection(), assembly, migrationSubType, store);
migration.Update();
} }
public object Load(Type type, UUID uuid) public object Load(Type type, UUID uuid)
{
using (IStatelessSession session = sessionFactory.OpenStatelessSession())
{ {
object obj = null; object obj = null;
try try
{ {
obj = session.Load(type, uuid); obj = session.Get(type.FullName, uuid);
} }
catch (Exception) catch (Exception)
{ {
m_log.ErrorFormat("[NHIBERNATE] {0} not found with ID {1} ", type.Name, uuid); m_log.ErrorFormat("[NHIBERNATE] {0} not found with ID {1} ", type.Name, uuid);
} }
return obj; return obj;
}
} }
@ -103,63 +114,80 @@ namespace OpenSim.Data.NHibernate
{ {
try try
{ {
session.BeginTransaction(); using (IStatelessSession session = sessionFactory.OpenStatelessSession())
session.Save(obj); {
session.Transaction.Commit(); using (ITransaction transaction=session.BeginTransaction())
session.Flush(); {
session.Insert(obj);
transaction.Commit();
return true; return true;
} }
}
}
catch (Exception e) catch (Exception e)
{ {
m_log.Error("[NHIBERNATE] issue saving object ", e); m_log.Error("[NHIBERNATE] issue inserting object ", e);
}
return false; return false;
} }
}
public bool Update(object obj) public bool Update(object obj)
{ {
try try
{ {
session.BeginTransaction(); using (IStatelessSession session = sessionFactory.OpenStatelessSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Update(obj); session.Update(obj);
session.Transaction.Commit(); transaction.Commit();
session.Flush();
return true; return true;
} }
}
}
catch (Exception e) catch (Exception e)
{ {
m_log.Error("[NHIBERNATE] issue updating object ", e); m_log.Error("[NHIBERNATE] issue updating object ", e);
}
return false; return false;
} }
}
public bool Delete(object obj) public bool Delete(object obj)
{ {
try try
{ {
session.BeginTransaction(); using (IStatelessSession session = sessionFactory.OpenStatelessSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Delete(obj); session.Delete(obj);
session.Transaction.Commit(); transaction.Commit();
session.Flush();
return true; return true;
} }
}
}
catch (Exception e) catch (Exception e)
{ {
m_log.Error("[NHIBERNATE] issue deleting object ", e); m_log.Error("[NHIBERNATE] issue deleting object ", e);
}
return false; return false;
} }
}
public void DropSchema() public void DropSchema()
{ {
SchemaExport export = new SchemaExport(this.cfg); SchemaExport export = new SchemaExport(this.configuration);
export.Drop(true, true); export.Drop(true, true);
using (ISession session = sessionFactory.OpenSession())
{
ISQLQuery sqlQuery=session.CreateSQLQuery("drop table migrations");
sqlQuery.ExecuteUpdate();
}
} }
public ISession GetSession() public ISession GetSession()
{ {
return session; return sessionFactory.OpenSession();
} }
} }
} }