@@ -31,7 +31,7 @@ import RoundRobinLoadBalancingStrategy, {ROUND_ROBIN_STRATEGY_NAME} from './inte
3131class RoutingDriver extends Driver {
3232
3333 constructor ( url , routingContext , userAgent , token = { } , config = { } ) {
34- super ( url , userAgent , token , RoutingDriver . _validateConfig ( config ) ) ;
34+ super ( url , userAgent , token , validateConfig ( config ) ) ;
3535 this . _routingContext = routingContext ;
3636 }
3737
@@ -42,16 +42,18 @@ class RoutingDriver extends Driver {
4242
4343 _createSession ( mode , connectionProvider , bookmark , config ) {
4444 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
4747 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 ) ;
5557 return newError ( 'No longer possible to write to server at ' + url , SESSION_EXPIRED ) ;
5658 } else {
5759 return error ;
@@ -65,30 +67,12 @@ class RoutingDriver extends Driver {
6567 return SESSION_EXPIRED ;
6668 }
6769
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-
8770 /**
8871 * Create new load balancing strategy based on the config.
8972 * @param {object } config the user provided config.
9073 * @param {Pool } connectionPool the connection pool for this driver.
9174 * @return {LoadBalancingStrategy } new strategy.
75+ * @private
9276 */
9377 static _createLoadBalancingStrategy ( config , connectionPool ) {
9478 const configuredValue = config . loadBalancingStrategy ;
@@ -102,6 +86,34 @@ class RoutingDriver extends Driver {
10286 }
10387}
10488
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+ */
105117class RoutingSession extends Session {
106118 constructor ( mode , connectionProvider , bookmark , config , onFailedConnection ) {
107119 super ( mode , connectionProvider , bookmark , config ) ;
0 commit comments