Skip to content

Commit bd6a15c

Browse files
author
ipolevoy
committed
#733 Implement ability to open and close a connection with Lambda
1 parent 9040254 commit bd6a15c

File tree

3 files changed

+334
-1
lines changed

3 files changed

+334
-1
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/Base.java

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,145 @@ public static void attach(Connection connection) {
367367
public static Connection detach() {
368368
return new DB(DB.DEFAULT_NAME).detach();
369369
}
370+
371+
/**
372+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
373+
* and then will close the connection. The connection to open is the same as in {@link #open(String, Properties)} method.
374+
*
375+
* <p></p>
376+
*
377+
* Example of usage:
378+
* <pre>
379+
withDb(connectionSpec, () -> {
380+
//place code here
381+
});
382+
* </pre>
383+
*
384+
* @param jndiName name of a configured data source.
385+
* @param jndiProperties JNDI properties.
386+
* @param runnable instance of <code>Runnable</code> to execute.
387+
*/
388+
public static void withDb(String jndiName, Properties jndiProperties, Runnable runnable) {
389+
try (DB db = open(jndiName, jndiProperties)){
390+
runnable.run();
391+
}
392+
}
393+
394+
395+
/**
396+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
397+
* and then will close the connection. The connection to open is the same as in {@link #open(DataSource)} method.
398+
*
399+
* <p></p>
400+
*
401+
* Example of usage:
402+
* <pre>
403+
withDb(datasource, () -> {
404+
//place code here
405+
});
406+
* </pre>
407+
*
408+
* @param dataSource instance of <code>DataSource</code> to get a connection from.
409+
* @param runnable instance of <code>Runnable</code> to execute.
410+
*/
411+
public static void withDb(DataSource dataSource, Runnable runnable) {
412+
try (DB db = open(dataSource)){
413+
runnable.run();
414+
}
415+
}
416+
417+
418+
/**
419+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
420+
* and then will close the connection. The connection to open is the same as in {@link #open(String)} method.
421+
*
422+
* <p></p>
423+
*
424+
* Example of usage:
425+
* <pre>
426+
withDb(jndiName, () -> {
427+
//place code here
428+
});
429+
* </pre>
430+
*
431+
* @param jndiName name of a JNDI connection from container
432+
* @param runnable instance of <code>Runnable</code> to execute.
433+
*/
434+
public static void withDb(String jndiName, Runnable runnable) {
435+
try (DB db = open(jndiName)){
436+
runnable.run();
437+
}
438+
}
439+
440+
/**
441+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
442+
* and then will close the connection. The connection to open is the same as in
443+
* {@link #open(String, String, Properties)} method.
444+
*
445+
* <p></p>
446+
*
447+
* Example of usage:
448+
* <pre>
449+
withDb(driver, url, properties, () -> {
450+
//place code here
451+
});
452+
* </pre>
453+
*
454+
* The arguments to this method are the same as to {@link #open(String, String, Properties)} method.
455+
*
456+
* @param runnable instance of <code>Runnable</code> to execute.
457+
*/
458+
public static void withDb(String driver, String url, Properties properties, Runnable runnable) {
459+
try (DB db = open(driver, url, properties)){
460+
runnable.run();
461+
}
462+
}
463+
464+
465+
466+
/**
467+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
468+
* and then will close the connection. The connection to open is the same as in
469+
* {@link #open(String, String, String, String)} method.
470+
*
471+
* <p></p>
472+
*
473+
* Example of usage:
474+
* <pre>
475+
withDb(driver, url, user, password, () -> {
476+
//place code here
477+
});
478+
* </pre>
479+
*
480+
* The arguments to this method are the same as to {@link #open(String, String, String, String)} method.
481+
*
482+
* @param runnable instance of <code>Runnable</code> to execute.
483+
*/
484+
public static void withDb(String driver, String url, String user, String password, Runnable runnable) {
485+
try (DB db = open(driver, url, user, password)){
486+
runnable.run();
487+
}
488+
}
489+
490+
/**
491+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
492+
* and then will close the connection. The connection to open is the same as in
493+
* {@link #open()} method.
494+
*
495+
* <p></p>
496+
*
497+
* Example of usage:
498+
* <pre>
499+
withDb(() -> {
500+
//place code here
501+
});
502+
* </pre>
503+
*
504+
* @param runnable instance of <code>Runnable</code> to execute.
505+
*/
506+
public void withDb(Runnable runnable) {
507+
try (DB db = open()){
508+
runnable.run();
509+
}
510+
}
370511
}

activejdbc/src/main/java/org/javalite/activejdbc/DB.java

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public DB open(String jndiName, Properties jndiProperties) {
228228

229229

230230
/**
231-
* This method is used internally by framework.
231+
* This method is used internally by the framework.
232232
*
233233
* @param spec specification for a JDBC connection.
234234
*/
@@ -869,4 +869,147 @@ private void setParameters(PreparedStatement ps, Object... params) throws SQLExc
869869
ps.setObject(index, param);
870870
}
871871
}
872+
873+
874+
/**
875+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
876+
* and then will close the connection. The connection to open is the same as in {@link #open(String, Properties)} method.
877+
*
878+
* <p></p>
879+
*
880+
* Example of usage:
881+
* <pre>
882+
withDb(connectionSpec, () -> {
883+
//place code here
884+
});
885+
* </pre>
886+
*
887+
* @param jndiName name of a configured data source.
888+
* @param jndiProperties JNDI properties.
889+
* @param runnable instance of <code>Runnable</code> to execute.
890+
*/
891+
public void withDb(String jndiName, Properties jndiProperties, Runnable runnable) {
892+
try (DB db = open(jndiName, jndiProperties)){
893+
runnable.run();
894+
}
895+
}
896+
897+
898+
/**
899+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
900+
* and then will close the connection. The connection to open is the same as in {@link #open(DataSource)} method.
901+
*
902+
* <p></p>
903+
*
904+
* Example of usage:
905+
* <pre>
906+
withDb(datasource, () -> {
907+
//place code here
908+
});
909+
* </pre>
910+
*
911+
* @param dataSource instance of <code>DataSource</code> to get a connection from.
912+
* @param runnable instance of <code>Runnable</code> to execute.
913+
*/
914+
public void withDb(DataSource dataSource, Runnable runnable) {
915+
try (DB db = open(dataSource)){
916+
runnable.run();
917+
}
918+
}
919+
920+
921+
/**
922+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
923+
* and then will close the connection. The connection to open is the same as in {@link #open(String)} method.
924+
*
925+
* <p></p>
926+
*
927+
* Example of usage:
928+
* <pre>
929+
withDb(jndiName, () -> {
930+
//place code here
931+
});
932+
* </pre>
933+
*
934+
* @param jndiName name of a JNDI connection from container
935+
* @param runnable instance of <code>Runnable</code> to execute.
936+
*/
937+
public void withDb(String jndiName, Runnable runnable) {
938+
try (DB db = open(jndiName)){
939+
runnable.run();
940+
}
941+
}
942+
943+
/**
944+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
945+
* and then will close the connection. The connection to open is the same as in
946+
* {@link #open(String, String, Properties)} method.
947+
*
948+
* <p></p>
949+
*
950+
* Example of usage:
951+
* <pre>
952+
withDb(driver, url, properties, () -> {
953+
//place code here
954+
});
955+
* </pre>
956+
*
957+
* The arguments to this method are the same as to {@link #open(String, String, Properties)} method.
958+
*
959+
* @param runnable instance of <code>Runnable</code> to execute.
960+
*/
961+
public void withDb(String driver, String url, Properties properties, Runnable runnable) {
962+
try (DB db = open(driver, url, properties)){
963+
runnable.run();
964+
}
965+
}
966+
967+
968+
969+
/**
970+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
971+
* and then will close the connection. The connection to open is the same as in
972+
* {@link #open(String, String, String, String)} method.
973+
*
974+
* <p></p>
975+
*
976+
* Example of usage:
977+
* <pre>
978+
withDb(driver, url, user, password, () -> {
979+
//place code here
980+
});
981+
* </pre>
982+
*
983+
* The arguments to this method are the same as to {@link #open(String, String, String, String)} method.
984+
*
985+
* @param runnable instance of <code>Runnable</code> to execute.
986+
*/
987+
public void withDb(String driver, String url, String user, String password, Runnable runnable) {
988+
try (DB db = open(driver, url, user, password)){
989+
runnable.run();
990+
}
991+
}
992+
993+
994+
/**
995+
* Convenience method to be used outside ActiveWeb. This method will open a connection, run the <code>Runnable</code>
996+
* and then will close the connection. The connection to open is the same as in
997+
* {@link #open()} method.
998+
*
999+
* <p></p>
1000+
*
1001+
* Example of usage:
1002+
* <pre>
1003+
withDb(() -> {
1004+
//place code here
1005+
});
1006+
* </pre>
1007+
*
1008+
* @param runnable instance of <code>Runnable</code> to execute.
1009+
*/
1010+
public void withDb(Runnable runnable) {
1011+
try (DB db = open()){
1012+
runnable.run();
1013+
}
1014+
}
8721015
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2009-2018 Igor Polevoy
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
18+
package org.javalite.activejdbc;
19+
20+
import org.javalite.common.Util;
21+
import org.junit.Test;
22+
23+
import java.io.IOException;
24+
import java.util.Properties;
25+
26+
import static org.javalite.test.jspec.JSpec.the;
27+
28+
29+
public class BaseTest2 {
30+
31+
@Test
32+
public void shouldOpenConnection() throws IOException {
33+
34+
Properties props = Util.readProperties("/database.properties");
35+
36+
DB db = new DB("test");
37+
the(db.hasConnection()).shouldBeFalse();
38+
39+
System.out.println(props);
40+
db.withDb(props.getProperty("development.test.driver"), props.getProperty("development.test.url"),
41+
props.getProperty("development.test.username"), props.getProperty("development.test.password"), () -> {
42+
43+
the(db.hasConnection()).shouldBeTrue();
44+
45+
});
46+
47+
the(Base.hasConnection()).shouldBeFalse();
48+
}
49+
}

0 commit comments

Comments
 (0)