From 80520206fcd4c8c85eb28b4800c42136365b164d Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Mon, 1 Dec 2008 18:42:14 +0000 Subject: * Unforunately it turns out not to be that simple. Revert the rest of r7560 for now. --- OpenSim/Data/NHibernate/NHibernateAssetData.cs | 77 +++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 8 deletions(-) (limited to 'OpenSim/Data/NHibernate/NHibernateAssetData.cs') diff --git a/OpenSim/Data/NHibernate/NHibernateAssetData.cs b/OpenSim/Data/NHibernate/NHibernateAssetData.cs index 90d41e3..6ee527e 100644 --- a/OpenSim/Data/NHibernate/NHibernateAssetData.cs +++ b/OpenSim/Data/NHibernate/NHibernateAssetData.cs @@ -33,7 +33,10 @@ using System.Text.RegularExpressions; using OpenMetaverse; using log4net; using NHibernate; +using NHibernate.Cfg; +using NHibernate.Expression; using NHibernate.Mapping.Attributes; +using NHibernate.Tool.hbm2ddl; using OpenSim.Framework; using Environment=NHibernate.Cfg.Environment; @@ -46,7 +49,9 @@ namespace OpenSim.Data.NHibernate { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private NHibernateManager manager; + private Configuration cfg; + private ISessionFactory factory; + private ISession session; override public void Dispose() { } @@ -57,23 +62,79 @@ namespace OpenSim.Data.NHibernate public override void Initialise(string connect) { + // Split out the dialect, driver, and connect string + char[] split = {';'}; + string[] parts = connect.Split(split, 3); + if (parts.Length != 3) + { + // TODO: make this a real exception type + throw new Exception("Malformed Inventory connection string '" + connect + "'"); + } + + string dialect = parts[0]; + + // NHibernate setup + cfg = new Configuration(); + cfg.SetProperty(Environment.ConnectionProvider, + "NHibernate.Connection.DriverConnectionProvider"); + cfg.SetProperty(Environment.Dialect, + "NHibernate.Dialect." + dialect); + cfg.SetProperty(Environment.ConnectionDriver, + "NHibernate.Driver." + parts[1]); + cfg.SetProperty(Environment.ConnectionString, parts[2]); + cfg.AddAssembly("OpenSim.Data.NHibernate"); + - m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateAssetData"); - manager = new NHibernateManager(connect, "AssetStore"); + + HbmSerializer.Default.Validate = true; + using (MemoryStream stream = + HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly())) + cfg.AddInputStream(stream); + + factory = cfg.BuildSessionFactory(); + session = factory.OpenSession(); + + // This actually does the roll forward assembly stuff + Assembly assem = GetType().Assembly; + Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "AssetStore"); + m.Update(); } override public AssetBase FetchAsset(UUID uuid) { - return (AssetBase)manager.Load(typeof(AssetBase), uuid); + try + { + return session.Load(typeof(AssetBase), uuid) as AssetBase; + } + catch (ObjectNotFoundException) + { + m_log.ErrorFormat("[NHIBERNATE] no such asset {0}", uuid); + return null; + } + catch (Exception e) + { + m_log.Error("[NHIBERNATE] unexpected exception: ", e); + return null; + } } private void Save(AssetBase asset) { - AssetBase temp = (AssetBase)manager.Load(typeof(AssetBase), asset.FullID); - if (temp == null) + try { - manager.Save(asset); + // a is not used anywhere? + // AssetBase a = session.Load(typeof(AssetBase), asset.FullID) as AssetBase; + session.Load(typeof(AssetBase), asset.FullID); + } + catch (ObjectNotFoundException) + { + session.Save(asset); + session.Flush(); + } + catch (Exception e) + { + m_log.Error("[NHIBERNATE] issue saving asset", e); } } @@ -86,7 +147,7 @@ namespace OpenSim.Data.NHibernate override public void UpdateAsset(AssetBase asset) { m_log.InfoFormat("[NHIBERNATE] updating asset {0}", asset.FullID); - manager.Update(asset); + Save(asset); } // private void LogAssetLoad(AssetBase asset) -- cgit v1.1