21
21
import java .util .List ;
22
22
23
23
import org .neo4j .driver .internal .net .BoltServerAddress ;
24
+ import org .neo4j .driver .v1 .AccessMode ;
24
25
import org .neo4j .driver .v1 .Record ;
25
26
import org .neo4j .driver .v1 .StatementResult ;
26
27
import org .neo4j .driver .v1 .exceptions .ClientException ;
27
28
import org .neo4j .driver .v1 .exceptions .ConnectionFailureException ;
28
29
import org .neo4j .driver .v1 .exceptions .NoSuchRecordException ;
29
- import org .neo4j .driver .v1 .exceptions .SessionExpiredException ;
30
30
import org .neo4j .driver .v1 .summary .ResultSummary ;
31
31
import org .neo4j .driver .v1 .util .Function ;
32
32
33
+ import static java .lang .String .format ;
34
+
35
+ import static org .neo4j .driver .internal .ClusteredNetworkSession .filterFailureToWrite ;
36
+ import static org .neo4j .driver .internal .ClusteredNetworkSession .sessionExpired ;
37
+
33
38
public class ClusteredStatementResult implements StatementResult
34
39
{
35
40
private final StatementResult delegate ;
41
+ private final AccessMode mode ;
36
42
private final BoltServerAddress address ;
37
43
private final ClusteredErrorHandler onError ;
38
44
39
- ClusteredStatementResult ( StatementResult delegate , BoltServerAddress address , ClusteredErrorHandler onError )
45
+ ClusteredStatementResult ( StatementResult delegate , AccessMode mode , BoltServerAddress address ,
46
+ ClusteredErrorHandler onError )
40
47
{
41
48
this .delegate = delegate ;
49
+ this .mode = mode ;
42
50
this .address = address ;
43
51
this .onError = onError ;
44
52
}
@@ -52,18 +60,11 @@ public List<String> keys()
52
60
}
53
61
catch ( ConnectionFailureException e )
54
62
{
55
- throw sessionExpired ( e );
63
+ throw sessionExpired ( e , onError , address );
56
64
}
57
65
catch ( ClientException e )
58
66
{
59
- if ( isFailedToWrite ( e ) )
60
- {
61
- throw failedWrite ();
62
- }
63
- else
64
- {
65
- throw e ;
66
- }
67
+ throw filterFailureToWrite ( e , mode , onError , address );
67
68
}
68
69
}
69
70
@@ -76,18 +77,11 @@ public boolean hasNext()
76
77
}
77
78
catch ( ConnectionFailureException e )
78
79
{
79
- throw sessionExpired ( e );
80
+ throw sessionExpired ( e , onError , address );
80
81
}
81
82
catch ( ClientException e )
82
83
{
83
- if ( isFailedToWrite ( e ) )
84
- {
85
- throw failedWrite ();
86
- }
87
- else
88
- {
89
- throw e ;
90
- }
84
+ throw filterFailureToWrite ( e , mode , onError , address );
91
85
}
92
86
}
93
87
@@ -100,18 +94,11 @@ public Record next()
100
94
}
101
95
catch ( ConnectionFailureException e )
102
96
{
103
- throw sessionExpired ( e );
97
+ throw sessionExpired ( e , onError , address );
104
98
}
105
99
catch ( ClientException e )
106
100
{
107
- if ( isFailedToWrite ( e ) )
108
- {
109
- throw failedWrite ();
110
- }
111
- else
112
- {
113
- throw e ;
114
- }
101
+ throw filterFailureToWrite ( e , mode , onError , address );
115
102
}
116
103
}
117
104
@@ -125,18 +112,11 @@ public Record single() throws NoSuchRecordException
125
112
}
126
113
catch ( ConnectionFailureException e )
127
114
{
128
- throw sessionExpired ( e );
115
+ throw sessionExpired ( e , onError , address );
129
116
}
130
117
catch ( ClientException e )
131
118
{
132
- if ( isFailedToWrite ( e ) )
133
- {
134
- throw failedWrite ();
135
- }
136
- else
137
- {
138
- throw e ;
139
- }
119
+ throw filterFailureToWrite ( e , mode , onError , address );
140
120
}
141
121
}
142
122
@@ -149,18 +129,11 @@ public Record peek()
149
129
}
150
130
catch ( ConnectionFailureException e )
151
131
{
152
- throw sessionExpired ( e );
132
+ throw sessionExpired ( e , onError , address );
153
133
}
154
134
catch ( ClientException e )
155
135
{
156
- if ( isFailedToWrite ( e ) )
157
- {
158
- throw failedWrite ();
159
- }
160
- else
161
- {
162
- throw e ;
163
- }
136
+ throw filterFailureToWrite ( e , mode , onError , address );
164
137
}
165
138
}
166
139
@@ -173,42 +146,28 @@ public List<Record> list()
173
146
}
174
147
catch ( ConnectionFailureException e )
175
148
{
176
- throw sessionExpired ( e );
149
+ throw sessionExpired ( e , onError , address );
177
150
}
178
151
catch ( ClientException e )
179
152
{
180
- if ( isFailedToWrite ( e ) )
181
- {
182
- throw failedWrite ();
183
- }
184
- else
185
- {
186
- throw e ;
187
- }
153
+ throw filterFailureToWrite ( e , mode , onError , address );
188
154
}
189
155
}
190
156
191
157
@ Override
192
- public <T > List <T > list ( Function <Record ,T > mapFunction )
158
+ public <T > List <T > list ( Function <Record , T > mapFunction )
193
159
{
194
160
try
195
161
{
196
- return delegate .list (mapFunction );
162
+ return delegate .list ( mapFunction );
197
163
}
198
164
catch ( ConnectionFailureException e )
199
165
{
200
- throw sessionExpired ( e );
166
+ throw sessionExpired ( e , onError , address );
201
167
}
202
168
catch ( ClientException e )
203
169
{
204
- if ( isFailedToWrite ( e ) )
205
- {
206
- throw failedWrite ();
207
- }
208
- else
209
- {
210
- throw e ;
211
- }
170
+ throw filterFailureToWrite ( e , mode , onError , address );
212
171
}
213
172
}
214
173
@@ -227,35 +186,12 @@ public ResultSummary consume()
227
186
}
228
187
catch ( ConnectionFailureException e )
229
188
{
230
- throw sessionExpired ( e );
189
+ throw sessionExpired ( e , onError , address );
231
190
}
232
191
catch ( ClientException e )
233
192
{
234
- if ( isFailedToWrite ( e ) )
235
- {
236
- throw failedWrite ();
237
- }
238
- else
239
- {
240
- throw e ;
241
- }
193
+ throw filterFailureToWrite ( e , mode , onError , address );
242
194
}
243
195
}
244
196
245
- private SessionExpiredException sessionExpired ( ConnectionFailureException e )
246
- {
247
- onError .onConnectionFailure ( address );
248
- return new SessionExpiredException ( String .format ( "Server at %s is no longer available" , address .toString ()), e );
249
- }
250
-
251
- private SessionExpiredException failedWrite ()
252
- {
253
- onError .onWriteFailure ( address );
254
- return new SessionExpiredException ( String .format ( "Server at %s no longer accepts writes" , address .toString ()));
255
- }
256
-
257
- private boolean isFailedToWrite ( ClientException e )
258
- {
259
- return e .code ().equals ( "Neo.ClientError.Cluster.NotALeader" );
260
- }
261
197
}
0 commit comments