@@ -61,36 +61,47 @@ public static Driver configuration() throws Exception
61
61
62
62
public static void statement ( Session session ) throws Exception
63
63
{
64
- // tag::statement[]
65
- StatementResult result =
66
- session .run ( "CREATE (person:Person {name: {name}})" , Values .parameters ( "name" , "Arthur" ) );
67
- // end::statement[]
68
- int theOnesCreated = result .consume ().counters ().nodesCreated ();
69
- System .out .println ( "There were " + theOnesCreated + " the ones created." );
64
+ try ( Transaction transaction = session .beginTransaction () )
65
+ {
66
+ // tag::statement[]
67
+ StatementResult result =
68
+ transaction .run ( "CREATE (person:Person {name: {name}})" , Values .parameters ( "name" , "Arthur" ) );
69
+ transaction .success ();
70
+ // end::statement[]
71
+ int theOnesCreated = result .consume ().counters ().nodesCreated ();
72
+ System .out .println ( "There were " + theOnesCreated + " the ones created." );
73
+ }
70
74
}
71
75
72
76
public static void statementWithoutParameters ( Session session ) throws Exception
73
77
{
74
- // tag::statement-without-parameters[]
75
- StatementResult result = session .run ( "CREATE (p:Person {name: 'Arthur'})" );
76
- // end::statement-without-parameters[]
77
- int theOnesCreated = result .consume ().counters ().nodesCreated ();
78
- System .out .println ( "There were " + theOnesCreated + " the ones created." );
78
+ try ( Transaction transaction = session .beginTransaction () )
79
+ {
80
+ // tag::statement-without-parameters[]
81
+ StatementResult result = transaction .run ( "CREATE (p:Person { name: 'Arthur' })" );
82
+ transaction .success ();
83
+ // end::statement-without-parameters[]
84
+ int theOnesCreated = result .consume ().counters ().nodesCreated ();
85
+ System .out .println ( "There were " + theOnesCreated + " the ones created." );
86
+ }
79
87
}
80
88
81
89
public static void resultTraversal ( Session session ) throws Exception
82
90
{
83
91
// tag::result-traversal[]
84
92
String searchTerm = "Sword" ;
85
- StatementResult result =
86
- session .run ( "MATCH (weapon:Weapon) WHERE weapon.name CONTAINS {term} RETURN weapon.name" ,
87
- Values .parameters ( "term" , searchTerm ) );
88
-
89
- System .out .println ( "List of weapons called " + searchTerm + ":" );
90
- while ( result .hasNext () )
93
+ try ( Transaction tx = session .beginTransaction () )
91
94
{
92
- Record record = result .next ();
93
- System .out .println ( record .get ( "weapon.name" ).asString () );
95
+ StatementResult result =
96
+ tx .run ( "MATCH (weapon:Weapon) WHERE weapon.name CONTAINS {term} RETURN weapon.name" ,
97
+ Values .parameters ( "term" , searchTerm ) );
98
+
99
+ System .out .println ( "List of weapons called " + searchTerm + ":" );
100
+ while ( result .hasNext () )
101
+ {
102
+ Record record = result .next ();
103
+ System .out .println ( record .get ( "weapon.name" ).asString () );
104
+ }
94
105
}
95
106
// end::result-traversal[]
96
107
}
@@ -99,37 +110,50 @@ public static void accessRecord( Session session ) throws Exception
99
110
{
100
111
// tag::access-record[]
101
112
String searchTerm = "Arthur" ;
102
- StatementResult result = session .run ( "MATCH (weapon:Weapon) WHERE weapon.owner CONTAINS {term} " +
103
- "RETURN weapon.name, weapon.material, weapon.size" ,
104
- Values .parameters ( "term" , searchTerm ) );
105
113
106
- System .out .println ( "List of weapons owned by " + searchTerm + ":" );
107
- while ( result .hasNext () )
114
+ try ( Transaction tx = session .beginTransaction () )
108
115
{
109
- Record record = result .next ();
110
- List <String > sword = new ArrayList <>();
111
- for ( String key : record .keys () )
116
+ StatementResult result = tx .run (
117
+ "MATCH (weapon:Weapon) WHERE weapon.owner CONTAINS {term} RETURN weapon.name, weapon.material, " +
118
+ "weapon.size" ,
119
+
120
+ Values .parameters ( "term" , searchTerm ) );
121
+
122
+ System .out .println ( "List of weapons owned by " + searchTerm + ":" );
123
+ while ( result .hasNext () )
112
124
{
113
- sword .add ( key + ": " + record .get ( key ) );
125
+ Record record = result .next ();
126
+ List <String > sword = new ArrayList <>();
127
+ for ( String key : record .keys () )
128
+ {
129
+ sword .add ( key + ": " + record .get ( key ) );
130
+ }
131
+ System .out .println ( sword );
114
132
}
115
- System .out .println ( sword );
116
133
}
117
134
// end::access-record[]
118
135
}
119
136
120
137
public static void retainResultsForNestedQuerying ( Session session ) throws Exception
121
138
{
122
139
// tag::nested-statements[]
123
- StatementResult result = session .run (
124
- "MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN id(knight) AS knight_id" ,
125
- Values .parameters ( "castle" , "Camelot" ) );
126
-
140
+ StatementResult result = null ;
141
+ try ( Transaction transaction = session .beginTransaction () )
142
+ {
143
+ result = transaction .run (
144
+ "MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN id(knight) AS knight_id" ,
145
+ Values .parameters ( "castle" , "Camelot" ) );
146
+ }
127
147
for ( Record record : result .list () )
128
148
{
129
- session .run ( "MATCH (knight) WHERE id(knight) = {id} " +
130
- "MATCH (king:Person) WHERE king.name = {king} " +
131
- "CREATE (knight)-[:DEFENDS]->(king)" ,
132
- Values .parameters ( "id" , record .get ( "knight_id" ), "king" , "Arthur" ) );
149
+ try ( Transaction tx = session .beginTransaction () )
150
+ {
151
+ tx .run ( "MATCH (knight) WHERE id(knight) = {id} " +
152
+ "MATCH (king:Person) WHERE king.name = {king} " +
153
+ "CREATE (knight)-[:DEFENDS]->(king)" ,
154
+ Values .parameters ( "id" , record .get ( "knight_id" ), "king" , "Arthur" ) );
155
+ tx .success ();
156
+ }
133
157
}
134
158
// end::nested-statements[]
135
159
}
@@ -140,11 +164,15 @@ public static void retainResultsForLaterProcessing( Driver driver ) throws Excep
140
164
List <Record > records ;
141
165
try ( Session session = driver .session () )
142
166
{
143
- StatementResult result = session .run (
144
- "MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN knight.name AS name" ,
145
- Values .parameters ( "castle" , "Camelot" ) );
146
167
147
- records = result .list ();
168
+ try ( Transaction tx = session .beginTransaction () )
169
+ {
170
+ StatementResult result = tx .run (
171
+ "MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN knight.name AS name" ,
172
+ Values .parameters ( "castle" , "Camelot" ) );
173
+
174
+ records = result .list ();
175
+ }
148
176
}
149
177
150
178
for ( Record record : records )
@@ -159,7 +187,10 @@ public static void handleCypherError( Session session ) throws Exception
159
187
// tag::handle-cypher-error[]
160
188
try
161
189
{
162
- session .run ( "This will cause a syntax error" ).consume ();
190
+ try ( Transaction tx = session .beginTransaction () )
191
+ {
192
+ tx .run ( "This will cause a syntax error" ).consume ();
193
+ }
163
194
}
164
195
catch ( ClientException e )
165
196
{
@@ -195,24 +226,30 @@ public static void transactionRollback( Session session ) throws Exception
195
226
public static void resultSummary ( Session session ) throws Exception
196
227
{
197
228
// tag::result-summary-query-profile[]
198
- StatementResult result = session .run ( "PROFILE MATCH (p:Person {name: {name}}) RETURN id(p)" ,
199
- Values .parameters ( "name" , "Arthur" ) );
229
+ try ( Transaction tx = session .beginTransaction () )
230
+ {
231
+ StatementResult result = tx .run ( "PROFILE MATCH (p:Person { name: {name} }) RETURN id(p)" ,
232
+ Values .parameters ( "name" , "Arthur" ) );
200
233
201
- ResultSummary summary = result .consume ();
234
+ ResultSummary summary = result .consume ();
202
235
203
- System .out .println ( summary .statementType () );
204
- System .out .println ( summary .profile () );
236
+ System .out .println ( summary .statementType () );
237
+ System .out .println ( summary .profile () );
238
+ }
205
239
// end::result-summary-query-profile[]
206
240
}
207
241
208
242
public static void notifications ( Session session ) throws Exception
209
243
{
210
244
// tag::result-summary-notifications[]
211
- ResultSummary summary = session .run ( "EXPLAIN MATCH (king), (queen) RETURN king, queen" ).consume ();
212
-
213
- for ( Notification notification : summary .notifications () )
245
+ try ( Transaction tx = session .beginTransaction () )
214
246
{
215
- System .out .println ( notification );
247
+ ResultSummary summary = tx .run ( "EXPLAIN MATCH (king), (queen) RETURN king, queen" ).consume ();
248
+
249
+ for ( Notification notification : summary .notifications () )
250
+ {
251
+ System .out .println ( notification );
252
+ }
216
253
}
217
254
// end::result-summary-notifications[]
218
255
}
@@ -230,10 +267,12 @@ public static Driver requireEncryption() throws Exception
230
267
public static Driver trustOnFirstUse () throws Exception
231
268
{
232
269
// tag::tls-trust-on-first-use[]
233
- Driver driver = GraphDatabase .driver ( "bolt://localhost:7687" , AuthTokens .basic ( "neo4j" , "neo4j" ), Config .build ()
234
- .withEncryptionLevel ( Config .EncryptionLevel .REQUIRED )
235
- .withTrustStrategy ( Config .TrustStrategy .trustOnFirstUse ( new File ( "/path/to/neo4j_known_hosts" ) ) )
236
- .toConfig () );
270
+ Driver driver =
271
+ GraphDatabase .driver ( "bolt://localhost:7687" , AuthTokens .basic ( "neo4j" , "neo4j" ), Config .build ()
272
+ .withEncryptionLevel ( Config .EncryptionLevel .REQUIRED )
273
+ .withTrustStrategy (
274
+ Config .TrustStrategy .trustOnFirstUse ( new File ( "/path/to/neo4j_known_hosts" ) ) )
275
+ .toConfig () );
237
276
// end::tls-trust-on-first-use[]
238
277
239
278
return driver ;
@@ -242,10 +281,12 @@ public static Driver trustOnFirstUse() throws Exception
242
281
public static Driver trustSignedCertificates () throws Exception
243
282
{
244
283
// tag::tls-signed[]
245
- Driver driver = GraphDatabase .driver ( "bolt://localhost:7687" , AuthTokens .basic ( "neo4j" , "neo4j" ), Config .build ()
246
- .withEncryptionLevel ( Config .EncryptionLevel .REQUIRED )
247
- .withTrustStrategy ( Config .TrustStrategy .trustCustomCertificateSignedBy ( new File ( "/path/to/ca-certificate.pem" ) ) )
248
- .toConfig () );
284
+ Driver driver =
285
+ GraphDatabase .driver ( "bolt://localhost:7687" , AuthTokens .basic ( "neo4j" , "neo4j" ), Config .build ()
286
+ .withEncryptionLevel ( Config .EncryptionLevel .REQUIRED )
287
+ .withTrustStrategy ( Config .TrustStrategy
288
+ .trustCustomCertificateSignedBy ( new File ( "/path/to/ca-certificate.pem" ) ) )
289
+ .toConfig () );
249
290
// end::tls-signed[]
250
291
251
292
return driver ;
0 commit comments