28
28
use Laudis \Neo4j \Databags \SessionConfiguration ;
29
29
use Laudis \Neo4j \Databags \Statement ;
30
30
use Laudis \Neo4j \Databags \TransactionConfiguration ;
31
+ use Laudis \Neo4j \Enum \AccessMode ;
31
32
use Laudis \Neo4j \Exception \Neo4jException ;
32
33
use Laudis \Neo4j \Types \CypherList ;
33
34
use Psr \Http \Message \UriInterface ;
@@ -70,7 +71,7 @@ public function __construct(
70
71
71
72
public function runStatements (iterable $ statements , ?TransactionConfiguration $ config = null ): CypherList
72
73
{
73
- return $ this ->beginInstantTransaction ()->runStatements ($ statements );
74
+ return $ this ->beginInstantTransaction ($ this -> config )->runStatements ($ statements );
74
75
}
75
76
76
77
public function openTransaction (iterable $ statements = null , ?TransactionConfiguration $ config = null ): UnmanagedTransactionInterface
@@ -90,16 +91,24 @@ public function run(string $statement, iterable $parameters = [], ?TransactionCo
90
91
91
92
public function writeTransaction (callable $ tsxHandler , ?TransactionConfiguration $ config = null )
92
93
{
94
+ $ config ??= TransactionConfiguration::default ();
95
+
93
96
return TransactionHelper::retry (
94
- fn () => $ this ->beginTransaction ([] , $ config ),
97
+ fn () => $ this ->startTransaction ( $ config , $ this -> config -> withAccessMode (AccessMode:: WRITE ()) ),
95
98
$ tsxHandler ,
96
- $ config ?? TransactionConfiguration:: default ()
99
+ $ config
97
100
);
98
101
}
99
102
100
103
public function readTransaction (callable $ tsxHandler , ?TransactionConfiguration $ config = null )
101
104
{
102
- return $ this ->writeTransaction ($ tsxHandler );
105
+ $ config ??= TransactionConfiguration::default ();
106
+
107
+ return TransactionHelper::retry (
108
+ fn () => $ this ->startTransaction ($ config , $ this ->config ->withAccessMode (AccessMode::READ ())),
109
+ $ tsxHandler ,
110
+ $ config
111
+ );
103
112
}
104
113
105
114
public function transaction (callable $ tsxHandler , ?TransactionConfiguration $ config = null )
@@ -110,22 +119,7 @@ public function transaction(callable $tsxHandler, ?TransactionConfiguration $con
110
119
public function beginTransaction (?iterable $ statements = null , ?TransactionConfiguration $ config = null ): UnmanagedTransactionInterface
111
120
{
112
121
$ config ??= TransactionConfiguration::default ();
113
- try {
114
- $ bolt = $ this ->acquireBolt ($ config );
115
-
116
- $ begin = $ bolt ->begin (['db ' => $ this ->config ->getDatabase ()]);
117
-
118
- if (!$ begin ) {
119
- throw new Neo4jException (new Vector ([new Neo4jError ('' , 'Cannot open new transaction ' )]));
120
- }
121
- } catch (Exception $ e ) {
122
- if ($ e instanceof Neo4jException) {
123
- throw $ e ;
124
- }
125
- throw new Neo4jException (new Vector ([new Neo4jError ('' , $ e ->getMessage ())]), $ e );
126
- }
127
-
128
- $ tsx = new BoltUnmanagedTransaction ($ this ->config ->getDatabase (), $ this ->formatter , $ bolt );
122
+ $ tsx = $ this ->startTransaction ($ config , $ this ->config );
129
123
130
124
$ tsx ->runStatements ($ statements ?? []);
131
125
@@ -135,16 +129,40 @@ public function beginTransaction(?iterable $statements = null, ?TransactionConfi
135
129
/**
136
130
* @return UnmanagedTransactionInterface<T>
137
131
*/
138
- private function beginInstantTransaction (): TransactionInterface
132
+ private function beginInstantTransaction (SessionConfiguration $ config ): TransactionInterface
139
133
{
140
- return new BoltUnmanagedTransaction ($ this ->config ->getDatabase (), $ this ->formatter , $ this ->acquireBolt (TransactionConfiguration::default ()));
134
+ return new BoltUnmanagedTransaction (
135
+ $ this ->config ->getDatabase (),
136
+ $ this ->formatter ,
137
+ $ this ->acquireBolt (TransactionConfiguration::default (), $ config )
138
+ );
141
139
}
142
140
143
- private function acquireBolt (TransactionConfiguration $ config ): Bolt
141
+ private function acquireBolt (TransactionConfiguration $ config, SessionConfiguration $ sessionConfig ): Bolt
144
142
{
145
- $ bolt = new Bolt ($ this ->pool ->acquire ($ this ->uri , $ this -> config ->getAccessMode (), $ this ->auth , $ config ));
143
+ $ bolt = new Bolt ($ this ->pool ->acquire ($ this ->uri , $ sessionConfig ->getAccessMode (), $ this ->auth , $ config ));
146
144
$ this ->auth ->authenticateBolt ($ bolt , $ this ->uri , $ this ->userAgent );
147
145
148
146
return $ bolt ;
149
147
}
148
+
149
+ private function startTransaction (TransactionConfiguration $ config , SessionConfiguration $ sessionConfig ): UnmanagedTransactionInterface
150
+ {
151
+ try {
152
+ $ bolt = $ this ->acquireBolt ($ config , $ sessionConfig );
153
+
154
+ $ begin = $ bolt ->begin (['db ' => $ this ->config ->getDatabase ()]);
155
+
156
+ if (!$ begin ) {
157
+ throw new Neo4jException (new Vector ([new Neo4jError ('' , 'Cannot open new transaction ' )]));
158
+ }
159
+ } catch (Exception $ e ) {
160
+ if ($ e instanceof Neo4jException) {
161
+ throw $ e ;
162
+ }
163
+ throw new Neo4jException (new Vector ([new Neo4jError ('' , $ e ->getMessage ())]), $ e );
164
+ }
165
+
166
+ return new BoltUnmanagedTransaction ($ this ->config ->getDatabase (), $ this ->formatter , $ bolt );
167
+ }
150
168
}
0 commit comments