@@ -31,7 +31,7 @@ import RoundRobinLoadBalancingStrategy, {ROUND_ROBIN_STRATEGY_NAME} from './inte
31
31
class RoutingDriver extends Driver {
32
32
33
33
constructor ( url , routingContext , userAgent , token = { } , config = { } ) {
34
- super ( url , userAgent , token , RoutingDriver . _validateConfig ( config ) ) ;
34
+ super ( url , userAgent , token , validateConfig ( config ) ) ;
35
35
this . _routingContext = routingContext ;
36
36
}
37
37
@@ -42,16 +42,18 @@ class RoutingDriver extends Driver {
42
42
43
43
_createSession ( mode , connectionProvider , bookmark , config ) {
44
44
return new RoutingSession ( mode , connectionProvider , bookmark , config , ( error , conn ) => {
45
- if ( error . code === SESSION_EXPIRED ) {
46
- this . _forgetConnection ( conn ) ;
45
+ if ( ! conn ) {
46
+ // connection can be undefined if error happened before connection was acquired
47
47
return error ;
48
- } else if ( RoutingDriver . _isFailureToWrite ( error ) ) {
49
- let url = 'UNKNOWN' ;
50
- // connection is undefined if error happened before connection was acquired
51
- if ( conn ) {
52
- url = conn . url ;
53
- this . _connectionProvider . forgetWriter ( conn . url ) ;
54
- }
48
+ }
49
+
50
+ const url = conn . url ;
51
+
52
+ if ( error . code === SESSION_EXPIRED || isDatabaseUnavailable ( error ) ) {
53
+ this . _connectionProvider . forget ( url ) ;
54
+ return error ;
55
+ } else if ( isFailureToWrite ( error ) ) {
56
+ this . _connectionProvider . forgetWriter ( url ) ;
55
57
return newError ( 'No longer possible to write to server at ' + url , SESSION_EXPIRED ) ;
56
58
} else {
57
59
return error ;
@@ -65,30 +67,12 @@ class RoutingDriver extends Driver {
65
67
return SESSION_EXPIRED ;
66
68
}
67
69
68
- _forgetConnection ( connection ) {
69
- // connection is undefined if error happened before connection was acquired
70
- if ( connection ) {
71
- this . _connectionProvider . forget ( connection . url ) ;
72
- }
73
- }
74
-
75
- static _validateConfig ( config ) {
76
- if ( config . trust === 'TRUST_ON_FIRST_USE' ) {
77
- throw newError ( 'The chosen trust mode is not compatible with a routing driver' ) ;
78
- }
79
- return config ;
80
- }
81
-
82
- static _isFailureToWrite ( error ) {
83
- return error . code === 'Neo.ClientError.Cluster.NotALeader' ||
84
- error . code === 'Neo.ClientError.General.ForbiddenOnReadOnlyDatabase' ;
85
- }
86
-
87
70
/**
88
71
* Create new load balancing strategy based on the config.
89
72
* @param {object } config the user provided config.
90
73
* @param {Pool } connectionPool the connection pool for this driver.
91
74
* @return {LoadBalancingStrategy } new strategy.
75
+ * @private
92
76
*/
93
77
static _createLoadBalancingStrategy ( config , connectionPool ) {
94
78
const configuredValue = config . loadBalancingStrategy ;
@@ -102,6 +86,34 @@ class RoutingDriver extends Driver {
102
86
}
103
87
}
104
88
89
+ /**
90
+ * @private
91
+ */
92
+ function validateConfig ( config ) {
93
+ if ( config . trust === 'TRUST_ON_FIRST_USE' ) {
94
+ throw newError ( 'The chosen trust mode is not compatible with a routing driver' ) ;
95
+ }
96
+ return config ;
97
+ }
98
+
99
+ /**
100
+ * @private
101
+ */
102
+ function isFailureToWrite ( error ) {
103
+ return error . code === 'Neo.ClientError.Cluster.NotALeader' ||
104
+ error . code === 'Neo.ClientError.General.ForbiddenOnReadOnlyDatabase' ;
105
+ }
106
+
107
+ /**
108
+ * @private
109
+ */
110
+ function isDatabaseUnavailable ( error ) {
111
+ return error . code === 'Neo.TransientError.General.DatabaseUnavailable' ;
112
+ }
113
+
114
+ /**
115
+ * @private
116
+ */
105
117
class RoutingSession extends Session {
106
118
constructor ( mode , connectionProvider , bookmark , config , onFailedConnection ) {
107
119
super ( mode , connectionProvider , bookmark , config ) ;
0 commit comments