forked from superquanter/quanter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPersistenceActor.cs
212 lines (192 loc) · 6.14 KB
/
PersistenceActor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using Akka.Actor;
using Akka.Event;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using Quanter.Trader.Messages;
using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Quanter.Persistence
{
/// <summary>
/// 处理的消息有
/// </summary>
public class PersistenceActor : UntypedActor
{
private readonly ILoggingAdapter _log = Logging.GetLogger(Context);
private ISessionFactory factory = null;
private Configuration configuration = null;
private ISession session = null;
public PersistenceActor()
{
_log.Debug("");
}
protected override void OnReceive(object message)
{
try {
PersistenceRequest pr = message as PersistenceRequest;
if (pr != null)
{
switch (pr.Type)
{
case PersistenceType.INIT_DATABASE:
_createTables();
break;
case PersistenceType.OPEN:
_log.Debug("初始化,并打开Session");
_init();
_openSession();
break;
case PersistenceType.SAVE:
_save(pr.Body);
break;
case PersistenceType.LOAD:
//_load((int)pr.Body);
break;
case PersistenceType.DELETE:
_delete(pr.Body);
break;
case PersistenceType.UPDATE:
_update(pr.Body);
break;
case PersistenceType.LIST:
_list((String)pr.Body);
break;
case PersistenceType.FIND:
_find((String)pr.Body);
break;
case PersistenceType.CLOSE:
_log.Debug("关闭Session");
// _closeSession();
break;
default:
break;
}
}
} catch (Exception e)
{
_log.Error("PersistenceActor.OnReceive 发生异常: {0}", e.StackTrace);
}
}
private void _createTables() {
try {
Configuration _cfg = new Configuration().Configure();
SchemaExport export = new SchemaExport(_cfg);
//export.SetOutputFile("./db/create_tables.sql");
export.Drop(true, true);
export.Create(false, true);
} catch (Exception e)
{
_log.Error("创建数据库语句发生异常。 {0}", e.StackTrace);
}
}
private void _init()
{
_log.Info("初始化Persistence");
try
{
configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(Assembly.GetExecutingAssembly());
factory = configuration.BuildSessionFactory();
}
catch (Exception e)
{
_log.Error("初始化HIBERNATE,发生异常:{0}", e.StackTrace);
}
}
private void _save(Object obj)
{
_log.Debug("保存对象 {0}", obj.GetType().ToString());
try {
session.Save(obj);
session.Flush();
Sender.Tell(obj);
} catch (Exception e)
{
_log.Error("保存发生异常 {0}", e.StackTrace);
}
}
private void _update(Object obj)
{
_log.Debug("更新对象 {0}", obj.GetType().ToString());
session.Update(obj);
session.Flush();
Sender.Tell(obj);
}
private void _delete(Type theType, int id)
{
Object obj = session.Load(theType, id);
_delete(obj);
}
private void _delete(Object obj)
{
session.Delete(obj);
session.Flush();
}
private void _load(Type theType, int id)
{
object ret = null;
ret = session.Load(theType, id);
Sender.Tell(ret);
}
private void _find(String where)
{
_log.Info("{1}Find 数据{0}", where, this.ToString());
try
{
object ret = null;
IList objs = session.CreateQuery(where).List();
if (objs != null && objs.Count != 0) ret = objs[0];
Sender.Tell(ret);
}catch (Exception e)
{
_log.Error("{1}发送异常{0}", e.StackTrace, this.ToString());
}
}
private void _list(string where)
{
IList ret = null;
try
{
IQuery q = session.CreateQuery(where);
ret = q.List();
}
catch (Exception e)
{
_log.Error("发生异常 {0}", e.StackTrace);
} finally
{
Sender.Tell(ret);
}
}
private ISession _openSession()
{
if (session == null)
session = factory.OpenSession();
session.FlushMode = FlushMode.Always;
return session;
}
private void _closeSession()
{
_log.Info("关闭数据库链接");
if (session != null)
{
session.Close();
session = null;
}
}
protected override void PreStart()
{
base.PreStart();
}
protected override void PostStop()
{
base.PostStop();
}
}
}