From aec5f0ceed08b3ac9f94752a7d18ffe73d5abbf2 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Fri, 18 Jul 2008 15:06:24 +0000 Subject: found a better pattern for SaveOrUpdate with nhibernate cleaned up the asset path a bit --- OpenSim/Data/NHibernate/NHibernateAssetData.cs | 45 ++++++++++++-------- OpenSim/Data/NHibernate/NHibernateRegionData.cs | 56 ++++++++++--------------- 2 files changed, 49 insertions(+), 52 deletions(-) (limited to 'OpenSim/Data') diff --git a/OpenSim/Data/NHibernate/NHibernateAssetData.cs b/OpenSim/Data/NHibernate/NHibernateAssetData.cs index eb4e14c..488cae4 100644 --- a/OpenSim/Data/NHibernate/NHibernateAssetData.cs +++ b/OpenSim/Data/NHibernate/NHibernateAssetData.cs @@ -26,6 +26,7 @@ */ using System; +using System.Collections; using System.IO; using System.Reflection; using System.Text.RegularExpressions; @@ -33,6 +34,7 @@ using libsecondlife; using log4net; using NHibernate; using NHibernate.Cfg; +using NHibernate.Expression; using NHibernate.Mapping.Attributes; using NHibernate.Tool.hbm2ddl; using OpenSim.Framework; @@ -105,36 +107,45 @@ namespace OpenSim.Data.NHibernate { return session.Load(typeof(AssetBase), uuid) as AssetBase; } + catch (ObjectNotFoundException e) + { + m_log.ErrorFormat("[NHIBERNATE] no such asset {0}", uuid); + return null; + } catch (Exception e) { - m_log.Error("[NHIBERNATE] issue loading asset", e); + m_log.Error("[NHIBERNATE] unexpected exception: ", e); return null; } } - override public void CreateAsset(AssetBase asset) + private void Save(AssetBase asset) { - if (!ExistsAsset(asset.FullID)) + try + { + AssetBase a = session.Load(typeof(AssetBase), asset.FullID) as AssetBase; + } + catch (ObjectNotFoundException e) + { + session.Save(asset); + session.Flush(); + } + catch (Exception e) { - m_log.InfoFormat("[NHIBERNATE] inserting asset {0}", asset.FullID); - using (ITransaction transaction = session.BeginTransaction()) - { - session.Save(asset); - transaction.Commit(); - } + m_log.Error("[NHIBERNATE] issue saving asset", e); } } + override public void CreateAsset(AssetBase asset) + { + m_log.InfoFormat("[NHIBERNATE] inserting asset {0}", asset.FullID); + Save(asset); + } + override public void UpdateAsset(AssetBase asset) { - if (ExistsAsset(asset.FullID)) - { - using (ITransaction transaction = session.BeginTransaction()) - { - session.Update(asset); - transaction.Commit(); - } - } + m_log.InfoFormat("[NHIBERNATE] updating asset {0}", asset.FullID); + Save(asset); } // private void LogAssetLoad(AssetBase asset) diff --git a/OpenSim/Data/NHibernate/NHibernateRegionData.cs b/OpenSim/Data/NHibernate/NHibernateRegionData.cs index 3d2d0e6..40861ae 100644 --- a/OpenSim/Data/NHibernate/NHibernateRegionData.cs +++ b/OpenSim/Data/NHibernate/NHibernateRegionData.cs @@ -114,31 +114,25 @@ namespace OpenSim.Data.NHibernate return null; } + // This looks inefficient, but it turns out that it isn't + // based on trial runs with nhibernate 1.2 private void SaveOrUpdate(SceneObjectPart p) { try { - ICriteria criteria = session.CreateCriteria(typeof(SceneObjectPart)); - criteria.Add(Expression.Eq("UUID", p.UUID)); - ArrayList l = (ArrayList)criteria.List(); - if (l.Count < 1) - { - session.Save(p); - } - else if (l.Count == 1) - { - SceneObjectPart old = (SceneObjectPart)l[0]; - session.Evict(old); - session.Update(p); - } - else - { - m_log.Error("Not unique"); - } + SceneObjectPart old = session.Load(typeof(SceneObjectPart), p.UUID) as SceneObjectPart; + session.Evict(old); + session.Update(p); + m_log.InfoFormat("[NHIBERNATE] updating object {0}", p.UUID); + } + catch (ObjectNotFoundException e) + { + m_log.InfoFormat("[NHIBERNATE] saving object {0}", p.UUID); + session.Save(p); } catch (Exception e) { - m_log.Error("[NHIBERNATE] issue saving prim", e); + m_log.Error("[NHIBERNATE] issue saving part", e); } } @@ -146,23 +140,15 @@ namespace OpenSim.Data.NHibernate { try { - ICriteria criteria = session.CreateCriteria(typeof(Terrain)); - criteria.Add(Expression.Eq("RegionID", t.RegionID)); - ArrayList l = (ArrayList)criteria.List(); - if (l.Count < 1) - { - session.Save(t); - } - else if (l.Count == 1) - { - Terrain old = (Terrain)l[0]; - session.Evict(old); - session.Update(t); - } - else - { - m_log.Error("Not unique"); - } + Terrain old = session.Load(typeof(Terrain), t.RegionID) as Terrain; + session.Evict(old); + session.Update(t); + session.Flush(); + } + catch (ObjectNotFoundException e) + { + session.Save(t); + session.Flush(); } catch (Exception e) { -- cgit v1.1