diff options
-rw-r--r-- | OpenSim/Data/NHibernate/NHibernateManager.cs | 118 |
1 files changed, 73 insertions, 45 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateManager.cs b/OpenSim/Data/NHibernate/NHibernateManager.cs index 12d8959..0692f26 100644 --- a/OpenSim/Data/NHibernate/NHibernateManager.cs +++ b/OpenSim/Data/NHibernate/NHibernateManager.cs | |||
@@ -42,9 +42,8 @@ namespace OpenSim.Data.NHibernate | |||
42 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 42 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
43 | 43 | ||
44 | private string dialect; | 44 | private string dialect; |
45 | private Configuration cfg; | 45 | private Configuration configuration; |
46 | private ISessionFactory factory; | 46 | private ISessionFactory sessionFactory; |
47 | private ISession session; | ||
48 | 47 | ||
49 | public NHibernateManager(string connect, string store) | 48 | public NHibernateManager(string connect, string store) |
50 | { | 49 | { |
@@ -61,41 +60,53 @@ namespace OpenSim.Data.NHibernate | |||
61 | dialect = parts[0]; | 60 | dialect = parts[0]; |
62 | 61 | ||
63 | // NHibernate setup | 62 | // NHibernate setup |
64 | cfg = new Configuration(); | 63 | configuration = new Configuration(); |
65 | cfg.SetProperty(Environment.ConnectionProvider, | 64 | configuration.SetProperty(Environment.ConnectionProvider, |
66 | "NHibernate.Connection.DriverConnectionProvider"); | 65 | "NHibernate.Connection.DriverConnectionProvider"); |
67 | cfg.SetProperty(Environment.Dialect, | 66 | configuration.SetProperty(Environment.Dialect, |
68 | "NHibernate.Dialect." + dialect); | 67 | "NHibernate.Dialect." + dialect); |
69 | cfg.SetProperty(Environment.ConnectionDriver, | 68 | configuration.SetProperty(Environment.ConnectionDriver, |
70 | "NHibernate.Driver." + parts[1]); | 69 | "NHibernate.Driver." + parts[1]); |
71 | cfg.SetProperty(Environment.ConnectionString, parts[2]); | 70 | configuration.SetProperty(Environment.ConnectionString, parts[2]); |
72 | cfg.AddAssembly("OpenSim.Data.NHibernate"); | 71 | configuration.AddAssembly("OpenSim.Data.NHibernate"); |
73 | 72 | ||
74 | //To create sql file uncomment code below and write the name of the file | 73 | //To create sql file uncomment code below and write the name of the file |
75 | //SchemaExport exp = new SchemaExport(cfg); | 74 | //SchemaExport exp = new SchemaExport(cfg); |
76 | //exp.SetOutputFile("nameofthefile.sql"); | 75 | //exp.SetOutputFile("nameofthefile.sql"); |
77 | //exp.Create(false, true); | 76 | //exp.Create(false, true); |
78 | 77 | ||
79 | factory = cfg.BuildSessionFactory(); | 78 | sessionFactory = configuration.BuildSessionFactory(); |
80 | session = factory.OpenSession(); | ||
81 | 79 | ||
82 | Assembly assem = GetType().Assembly; | 80 | Assembly assembly = GetType().Assembly; |
83 | Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, store); | 81 | |
84 | m.Update(); | 82 | // Migration subtype is the folder name under which migrations are stored. For mysql this folder is |
83 | // MySQLDialect instead of MySQL5Dialect which is the dialect currently in use. To avoid renaming | ||
84 | // this folder each time the mysql version changes creating simple mapping: | ||
85 | String migrationSubType = dialect; | ||
86 | if (dialect.StartsWith("MySQL")) | ||
87 | { | ||
88 | migrationSubType="MySQLDialect"; | ||
89 | } | ||
90 | |||
91 | Migration migration = new Migration((System.Data.Common.DbConnection)sessionFactory.ConnectionProvider.GetConnection(), assembly, migrationSubType, store); | ||
92 | migration.Update(); | ||
85 | } | 93 | } |
86 | 94 | ||
87 | public object Load(Type type, UUID uuid) | 95 | public object Load(Type type, UUID uuid) |
88 | { | 96 | { |
89 | object obj = null; | 97 | using (IStatelessSession session = sessionFactory.OpenStatelessSession()) |
90 | try | ||
91 | { | 98 | { |
92 | obj = session.Load(type, uuid); | 99 | object obj = null; |
100 | try | ||
101 | { | ||
102 | obj = session.Get(type.FullName, uuid); | ||
103 | } | ||
104 | catch (Exception) | ||
105 | { | ||
106 | m_log.ErrorFormat("[NHIBERNATE] {0} not found with ID {1} ", type.Name, uuid); | ||
107 | } | ||
108 | return obj; | ||
93 | } | 109 | } |
94 | catch (Exception) | ||
95 | { | ||
96 | m_log.ErrorFormat("[NHIBERNATE] {0} not found with ID {1} ", type.Name, uuid); | ||
97 | } | ||
98 | return obj; | ||
99 | 110 | ||
100 | } | 111 | } |
101 | 112 | ||
@@ -103,63 +114,80 @@ namespace OpenSim.Data.NHibernate | |||
103 | { | 114 | { |
104 | try | 115 | try |
105 | { | 116 | { |
106 | session.BeginTransaction(); | 117 | using (IStatelessSession session = sessionFactory.OpenStatelessSession()) |
107 | session.Save(obj); | 118 | { |
108 | session.Transaction.Commit(); | 119 | using (ITransaction transaction=session.BeginTransaction()) |
109 | session.Flush(); | 120 | { |
110 | return true; | 121 | session.Insert(obj); |
122 | transaction.Commit(); | ||
123 | return true; | ||
124 | } | ||
125 | } | ||
111 | } | 126 | } |
112 | catch (Exception e) | 127 | catch (Exception e) |
113 | { | 128 | { |
114 | m_log.Error("[NHIBERNATE] issue saving object ", e); | 129 | m_log.Error("[NHIBERNATE] issue inserting object ", e); |
130 | return false; | ||
115 | } | 131 | } |
116 | return false; | ||
117 | } | 132 | } |
118 | 133 | ||
119 | public bool Update(object obj) | 134 | public bool Update(object obj) |
120 | { | 135 | { |
121 | try | 136 | try |
122 | { | 137 | { |
123 | session.BeginTransaction(); | 138 | using (IStatelessSession session = sessionFactory.OpenStatelessSession()) |
124 | session.Update(obj); | 139 | { |
125 | session.Transaction.Commit(); | 140 | using (ITransaction transaction = session.BeginTransaction()) |
126 | session.Flush(); | 141 | { |
127 | return true; | 142 | session.Update(obj); |
143 | transaction.Commit(); | ||
144 | return true; | ||
145 | } | ||
146 | } | ||
128 | } | 147 | } |
129 | catch (Exception e) | 148 | catch (Exception e) |
130 | { | 149 | { |
131 | m_log.Error("[NHIBERNATE] issue updating object ", e); | 150 | m_log.Error("[NHIBERNATE] issue updating object ", e); |
151 | return false; | ||
132 | } | 152 | } |
133 | return false; | ||
134 | } | 153 | } |
135 | 154 | ||
136 | public bool Delete(object obj) | 155 | public bool Delete(object obj) |
137 | { | 156 | { |
138 | try | 157 | try |
139 | { | 158 | { |
140 | session.BeginTransaction(); | 159 | using (IStatelessSession session = sessionFactory.OpenStatelessSession()) |
141 | session.Delete(obj); | 160 | { |
142 | session.Transaction.Commit(); | 161 | using (ITransaction transaction = session.BeginTransaction()) |
143 | session.Flush(); | 162 | { |
144 | return true; | 163 | session.Delete(obj); |
164 | transaction.Commit(); | ||
165 | return true; | ||
166 | } | ||
167 | } | ||
145 | } | 168 | } |
146 | catch (Exception e) | 169 | catch (Exception e) |
147 | { | 170 | { |
148 | |||
149 | m_log.Error("[NHIBERNATE] issue deleting object ", e); | 171 | m_log.Error("[NHIBERNATE] issue deleting object ", e); |
172 | return false; | ||
150 | } | 173 | } |
151 | return false; | ||
152 | } | 174 | } |
153 | 175 | ||
154 | public void DropSchema() | 176 | public void DropSchema() |
155 | { | 177 | { |
156 | SchemaExport export = new SchemaExport(this.cfg); | 178 | SchemaExport export = new SchemaExport(this.configuration); |
157 | export.Drop(true, true); | 179 | export.Drop(true, true); |
180 | |||
181 | using (ISession session = sessionFactory.OpenSession()) | ||
182 | { | ||
183 | ISQLQuery sqlQuery=session.CreateSQLQuery("drop table migrations"); | ||
184 | sqlQuery.ExecuteUpdate(); | ||
185 | } | ||
158 | } | 186 | } |
159 | 187 | ||
160 | public ISession GetSession() | 188 | public ISession GetSession() |
161 | { | 189 | { |
162 | return session; | 190 | return sessionFactory.OpenSession(); |
163 | } | 191 | } |
164 | } | 192 | } |
165 | } | 193 | } |