1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Data.Linq;
|
---|
24 | using System.Transactions;
|
---|
25 | using HeuristicLab.Services.Hive.DataAccess.Daos;
|
---|
26 | using HeuristicLab.Services.Hive.DataAccess.Daos.HiveStatistics;
|
---|
27 | using HeuristicLab.Services.Hive.DataAccess.Interfaces;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Services.Hive.DataAccess.Manager {
|
---|
30 | public class PersistenceManager : IPersistenceManager {
|
---|
31 | private readonly DataContext dataContext;
|
---|
32 | public DataContext DataContext {
|
---|
33 | get { return dataContext; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | #region Hive daos
|
---|
37 | public AssignedResourceDao AssignedResourceDao {
|
---|
38 | get { return new AssignedResourceDao(dataContext); }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public DowntimeDao DowntimeDao {
|
---|
42 | get { return new DowntimeDao(dataContext); }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public JobDao JobDao {
|
---|
46 | get { return new JobDao(dataContext); }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public JobPermissionDao JobPermissionDao {
|
---|
50 | get { return new JobPermissionDao(dataContext); }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public LifecycleDao LifecycleDao {
|
---|
54 | get { return new LifecycleDao(dataContext); }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public PluginDao PluginDao {
|
---|
58 | get { return new PluginDao(dataContext); }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public PluginDataDao PluginDataDao {
|
---|
62 | get { return new PluginDataDao(dataContext); }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public RequiredPluginDao RequiredPluginDao {
|
---|
66 | get { return new RequiredPluginDao(dataContext); }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public ResourceDao ResourceDao {
|
---|
70 | get { return new ResourceDao(dataContext); }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public ResourcePermissionDao ResourcePermissionDao {
|
---|
74 | get { return new ResourcePermissionDao(dataContext); }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public SlaveDao SlaveDao {
|
---|
78 | get { return new SlaveDao(dataContext); }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public SlaveGroupDao SlaveGroupDao {
|
---|
82 | get { return new SlaveGroupDao(dataContext); }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public StateLogDao StateLogDao {
|
---|
86 | get { return new StateLogDao(dataContext); }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public TaskDao TaskDao {
|
---|
90 | get { return new TaskDao(dataContext); }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public TaskDataDao TaskDataDao {
|
---|
94 | get { return new TaskDataDao(dataContext); }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public UserPriorityDao UserPriorityDao {
|
---|
98 | get { return new UserPriorityDao(dataContext); }
|
---|
99 | }
|
---|
100 | #endregion
|
---|
101 |
|
---|
102 | #region HiveStatistics daos
|
---|
103 | public DimClientDao DimClientDao {
|
---|
104 | get { return new DimClientDao(dataContext); }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public DimJobDao DimJobDao {
|
---|
108 | get { return new DimJobDao(dataContext); }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public DimTimeDao DimTimeDao {
|
---|
112 | get { return new DimTimeDao(dataContext); }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public DimUserDao DimUserDao {
|
---|
116 | get { return new DimUserDao(dataContext); }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public FactClientInfoDao FactClientInfoDao {
|
---|
120 | get { return new FactClientInfoDao(dataContext); }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public FactTaskDao FactTaskDao {
|
---|
124 | get { return new FactTaskDao(dataContext); }
|
---|
125 | }
|
---|
126 | #endregion
|
---|
127 |
|
---|
128 | public PersistenceManager(bool longRunning = false) {
|
---|
129 | var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
|
---|
130 | if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
|
---|
131 | dataContext = context;
|
---|
132 | }
|
---|
133 |
|
---|
134 | #region Transaction management
|
---|
135 | public void UseTransaction(Action call, bool repeatableRead = false, bool longRunning = false) {
|
---|
136 | UseTransaction<object>(() => {
|
---|
137 | call();
|
---|
138 | return null;
|
---|
139 | });
|
---|
140 | }
|
---|
141 |
|
---|
142 | public T UseTransaction<T>(Func<T> call, bool repeatableRead = false, bool longRunning = false) {
|
---|
143 | int n = 10;
|
---|
144 | while (n > 0) {
|
---|
145 | TransactionScope transaction = CreateTransaction(repeatableRead, longRunning);
|
---|
146 | try {
|
---|
147 | T result = call();
|
---|
148 | transaction.Complete();
|
---|
149 | return result;
|
---|
150 | }
|
---|
151 | catch (System.Data.SqlClient.SqlException e) {
|
---|
152 | n--; // probably deadlock situation, let it roll back and repeat the transaction n times
|
---|
153 | LogFactory.GetLogger(typeof(TransactionManager).Namespace).Log(string.Format("Exception occured, repeating transaction {0} more times. Details: {1}", n, e.ToString()));
|
---|
154 | if (n <= 0) throw;
|
---|
155 | }
|
---|
156 | finally {
|
---|
157 | transaction.Dispose();
|
---|
158 | }
|
---|
159 | }
|
---|
160 | throw new Exception("Transaction couldn't be completed.");
|
---|
161 | }
|
---|
162 |
|
---|
163 | private static TransactionScope CreateTransaction(bool repeatableRead, bool longRunning) {
|
---|
164 | var options = new TransactionOptions {
|
---|
165 | IsolationLevel = repeatableRead ? IsolationLevel.RepeatableRead : IsolationLevel.ReadUncommitted
|
---|
166 | };
|
---|
167 | if (longRunning) {
|
---|
168 | options.Timeout = Settings.Default.LongRunningDatabaseCommandTimeout;
|
---|
169 | }
|
---|
170 | return new TransactionScope(TransactionScopeOption.Required, options);
|
---|
171 | }
|
---|
172 | #endregion
|
---|
173 |
|
---|
174 | public void SubmitChanges() {
|
---|
175 | if (dataContext != null) {
|
---|
176 | dataContext.SubmitChanges();
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | public void Dispose() {
|
---|
181 | if (dataContext != null) {
|
---|
182 | dataContext.Dispose();
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|