-
Notifications
You must be signed in to change notification settings - Fork 1
/
DashboardDSTxBean.java
267 lines (236 loc) · 8.01 KB
/
DashboardDSTxBean.java
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.ea.ejbconcurrency.ejbimpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.sql.DataSource;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.ea.ejbconcurrency.dto.SegmentDTO;
import com.ea.ejbconcurrency.exception.ServerException;
import com.ea.ejbconcurrency.remote.DashboardRemote;
import com.ea.ejbconcurrency.util.DBUtil;
@Stateless
@Remote(DashboardRemote.class)
@TransactionManagement(TransactionManagementType.BEAN)
public class DashboardDSTxBean implements DashboardRemote {
private static final Log log = LogFactory.getLog(DashboardDSTxBean.class);
@Resource(mappedName = "java:jboss/datasources/ExampleDS")
private DataSource dataSource;
@Resource
private UserTransaction tx;
public SegmentDTO addPointToDashboard(String newPoint) throws ServerException {
SegmentDTO segment = null;
Connection connection = null;
try {
connection = dataSource.getConnection();
connection.setAutoCommit(false);
// connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
tx.begin();
log.info("Inserting point '" + newPoint + "'. If present any free (uncoupled) point, a new segment between two nodes will be created. ");
String freePoint = getFirstFreePoint(connection);
if (freePoint != null) {
Thread.sleep(1000 * 2);
segment = insertSegment(freePoint, newPoint, connection);
log.info("Inserted segment: " + segment);
removePoint(freePoint, connection);
log.info("Removed temp point: " + freePoint);
} else {
log.info("No free points present for coupling, inserting '" + newPoint + "' in free points list");
insertPoint(newPoint, connection);
}
tx.commit();
// connection.commit();
} catch (NotSupportedException | InterruptedException
| SQLException
| SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException | SystemException e) {
try {
tx.rollback();
} catch (SecurityException | IllegalStateException | SystemException e1) {
log.error("Error tx.commit();", e1);
}
try {
connection.rollback();
} catch (SQLException e1) {
log.error("Error rollback exception while inserting point '" + newPoint + "'", e1);
}
log.error("General exception while inserting point '" + newPoint + "'", e);
throw new ServerException("Exception inserting point'" + newPoint + "': " + e.getMessage());
} finally {
DBUtil.close(connection);
}
return segment;
}
@Override
public List<SegmentDTO> getAllSegments() throws ServerException {
List<SegmentDTO> res = new ArrayList<>();
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select segment_id, point1, point2 from SEGMENT";
log.debug("getAllSegments sql: " + sql);
try {
connection = dataSource.getConnection();
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
SegmentDTO segment = null;
while (rs.next()) {
segment = new SegmentDTO();
segment.setId(rs.getInt("segment_id"));
segment.setPoint1(rs.getString("point1"));
segment.setPoint2(rs.getString("point2"));
res.add(segment);
}
// log.debug("Nr segments found: " + res.size());
log.debug("Segments in DB: " + res);
} catch (SQLException e) {
throw new ServerException("Exception in getAllSegments()", e);
} finally {
DBUtil.close(rs);
DBUtil.close(ps);
DBUtil.close(connection);
}
return res;
}
public List<String> getAllPoints() throws ServerException {
List<String> res = new ArrayList<>();
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select name from POINT";
log.debug("getAllPoints sql: " + sql);
try {
connection = dataSource.getConnection();
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
String point = null;
while (rs.next()) {
point = rs.getString("name");
res.add(point);
}
log.debug("Points in DB: " + res);
} catch (SQLException e) {
throw new ServerException("Exception in getAllPoints()", e);
} finally {
DBUtil.close(rs);
DBUtil.close(ps);
DBUtil.close(connection);
}
return res;
}
@Override
public void cleanDashboard() throws ServerException {
try {
removeAllSegments();
removeAllPoints();
log.info("Removed all Points and Segments");
} catch (Exception e) {
log.error("General exception while cleaning the Dashboard", e);
throw new ServerException("General exception while cleaning the Dashboard", e);
}
}
private void insertPoint(String newPoint, Connection connection) throws SQLException {
PreparedStatement ps = null;
int tot = 0;
String sql = "insert into POINT values(?)";
log.debug("insertPoint sql: " + sql + "; newPoint: " + newPoint);
ps = connection.prepareStatement(sql);
ps.setString(1, newPoint);
tot = ps.executeUpdate();
log.info("Inserted " + tot + " row");
DBUtil.close(ps);
}
private String getFirstFreePoint(Connection connection) throws SQLException {
String freePoint = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select name from POINT";
log.debug("getFirstFreePoint sql: " + sql);
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()) {
freePoint = rs.getString("name");
}
log.info("Free point found: " + freePoint);
return freePoint;
}
private void removePoint(String point, Connection connection) throws SQLException {
PreparedStatement ps = null;
int tot = 0;
String sql = "delete from POINT where name = ? ";
log.debug("removePoint sql: " + sql + "; point: " + point);
ps = connection.prepareStatement(sql);
ps.setString(1, point);
tot = ps.executeUpdate();
log.info("Deleted " + tot + " segments");
}
private void removeAllPoints() {
Connection connection = null;
PreparedStatement ps = null;
int tot = 0;
String sql = "delete from POINT";
log.debug("removeAllPoints sql: " + sql);
try {
connection = dataSource.getConnection();
ps = connection.prepareStatement(sql);
tot = ps.executeUpdate();
log.info("Deleted " + tot + " segments");
} catch (SQLException e) {
throw new ServerException("Exception in removeAllPoint()", e);
} finally {
DBUtil.close(ps);
DBUtil.close(connection);
}
}
private SegmentDTO insertSegment(String point1, String point2, Connection connection) {
PreparedStatement ps = null;
SegmentDTO ret = null;
String sql = "insert into SEGMENT values(NULL, ?, ?)";
log.debug("insertSegment sql: " + sql + "; point1: " + point1 + ", point2: " + point2);
try {
ps = connection.prepareStatement(sql);
ps.setString(1, point1);
ps.setString(2, point2);
int tot = ps.executeUpdate();
log.info("Inserted " + tot + " row");
ret = new SegmentDTO(point1, point2);
} catch (SQLException e) {
throw new ServerException(e);
} finally {
DBUtil.close(ps);
}
return ret;
}
private void removeAllSegments() {
Connection connection = null;
PreparedStatement ps = null;
int tot = 0;
String sql = "delete from SEGMENT";
log.debug("removeAllSegments sql: " + sql);
try {
connection = dataSource.getConnection();
ps = connection.prepareStatement(sql);
tot = ps.executeUpdate();
log.info("Deleted " + tot + " segments");
} catch (SQLException e) {
throw new ServerException("Exception in removeAllSegments()", e);
} finally {
DBUtil.close(ps);
DBUtil.close(connection);
}
}
}