Skip to content

Commit ea72539

Browse files
author
mpv1989
committed
added connection pooling (issue #103)
1 parent f4f1fbc commit ea72539

File tree

6 files changed

+53
-30
lines changed

6 files changed

+53
-30
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ v4.1.11 (2017-03-xx)
33
* added convenience methods (ArangoDatabase.arango(), ArangoCollection.db(), ArangoGraph.db())
44
* added convenience methods (ArangoCollection.getIndex(String), .deleteIndex(key))
55
* fixed exception handling in Connection (issue #110)
6+
* added connection pooling (issue #103)
67

78
v4.1.10 (2017-02-22)
89
---------------------------

src/main/java/com/arangodb/ArangoDBAsync.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public static class Builder {
8383
private Boolean useSsl;
8484
private SSLContext sslContext;
8585
private Integer chunksize;
86+
private Integer maxConnections;
8687
private final VPack.Builder vpackBuilder;
8788
private final CollectionCache collectionCache;
8889
private final VPackParser vpackParser;
@@ -113,6 +114,7 @@ public Builder loadProperties(final InputStream in) {
113114
password = loadPassword(properties, password);
114115
useSsl = loadUseSsl(properties, useSsl);
115116
chunksize = loadChunkSize(properties, chunksize);
117+
maxConnections = loadMaxConnections(properties, maxConnections);
116118
} catch (final IOException e) {
117119
throw new ArangoDBException(e);
118120
}
@@ -188,6 +190,11 @@ public Builder chunksize(final Integer chunksize) {
188190
return this;
189191
}
190192

193+
public Builder maxConnections(final Integer maxConnections) {
194+
this.maxConnections = maxConnections;
195+
return this;
196+
}
197+
191198
public <T> Builder registerSerializer(final Class<T> clazz, final VPackSerializer<T> serializer) {
192199
vpackBuilder.registerSerializer(clazz, serializer);
193200
return this;
@@ -269,12 +276,12 @@ public ArangoDBAsync build() {
269276

270277
private CommunicationAsync.Builder asyncBuilder(final HostHandler hostHandler) {
271278
return new CommunicationAsync.Builder(hostHandler).timeout(timeout).user(user).password(password)
272-
.useSsl(useSsl).sslContext(sslContext).chunksize(chunksize);
279+
.useSsl(useSsl).sslContext(sslContext).chunksize(chunksize).maxConnections(maxConnections);
273280
}
274281

275282
private CommunicationSync.Builder syncBuilder(final HostHandler hostHandler) {
276283
return new CommunicationSync.Builder(hostHandler).timeout(timeout).user(user).password(password)
277-
.useSsl(useSsl).sslContext(sslContext).chunksize(chunksize);
284+
.useSsl(useSsl).sslContext(sslContext).chunksize(chunksize).maxConnections(maxConnections);
278285
}
279286

280287
}
@@ -294,6 +301,7 @@ public ArangoDatabase db(final String name) {
294301
});
295302
}
296303

304+
@Override
297305
protected ArangoExecutorAsync executor() {
298306
return executor;
299307
}

src/main/java/com/arangodb/internal/velocystream/CommunicationAsync.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static class Builder {
5656
private Boolean useSsl;
5757
private SSLContext sslContext;
5858
private Integer chunksize;
59+
private Integer maxConnections;
5960

6061
public Builder(final HostHandler hostHandler) {
6162
super();
@@ -92,29 +93,41 @@ public Builder chunksize(final Integer chunksize) {
9293
return this;
9394
}
9495

96+
public Builder maxConnections(final Integer maxConnections) {
97+
this.maxConnections = maxConnections;
98+
return this;
99+
}
100+
95101
public Communication<CompletableFuture<Response>, ConnectionAsync> build(
96102
final VPack vpack,
97103
final CollectionCache collectionCache) {
98104
return new CommunicationAsync(hostHandler, timeout, user, password, useSsl, sslContext, vpack,
99-
collectionCache, chunksize);
105+
collectionCache, chunksize, maxConnections);
100106
}
101107
}
102108

103109
private CommunicationAsync(final HostHandler hostHandler, final Integer timeout, final String user,
104110
final String password, final Boolean useSsl, final SSLContext sslContext, final VPack vpack,
105-
final CollectionCache collectionCache, final Integer chunksize) {
111+
final CollectionCache collectionCache, final Integer chunksize, final Integer maxConnections) {
106112
super(timeout, user, password, useSsl, sslContext, vpack, collectionCache, chunksize,
107-
new ConnectionAsync.Builder(hostHandler, new MessageStore()).timeout(timeout).useSsl(useSsl)
108-
.sslContext(sslContext).build());
113+
new ConnectionPool<ConnectionAsync>(maxConnections) {
114+
private final ConnectionAsync.Builder builder = new ConnectionAsync.Builder(hostHandler,
115+
new MessageStore()).timeout(timeout).useSsl(useSsl).sslContext(sslContext);
116+
117+
@Override
118+
public ConnectionAsync createConnection() {
119+
return builder.build();
120+
}
121+
});
109122
}
110123

111124
@Override
112-
public CompletableFuture<Response> execute(final Request request) {
125+
public CompletableFuture<Response> execute(final Request request, final ConnectionAsync connection) {
113126
connect(connection);
114127
final CompletableFuture<Response> rfuture = new CompletableFuture<>();
115128
try {
116129
final Message message = createMessage(request);
117-
send(message).whenComplete((m, ex) -> {
130+
send(message, connection).whenComplete((m, ex) -> {
118131
if (m != null) {
119132
try {
120133
collectionCache.setDb(request.getDatabase());
@@ -151,7 +164,8 @@ public CompletableFuture<Response> execute(final Request request) {
151164
return rfuture;
152165
}
153166

154-
private CompletableFuture<Message> send(final Message message) throws IOException {
167+
private CompletableFuture<Message> send(final Message message, final ConnectionAsync connection)
168+
throws IOException {
155169
if (LOGGER.isDebugEnabled()) {
156170
LOGGER.debug(String.format("Send Message (id=%s, head=%s, body=%s)", message.getId(), message.getHead(),
157171
message.getBody() != null ? message.getBody() : "{}"));
@@ -160,12 +174,12 @@ private CompletableFuture<Message> send(final Message message) throws IOExceptio
160174
}
161175

162176
@Override
163-
protected void authenticate() {
177+
protected void authenticate(final ConnectionAsync connection) {
164178
Response response = null;
165179
try {
166180
response = execute(
167-
new AuthenticationRequest(user, password != null ? password : "", ArangoDBConstants.ENCRYPTION_PLAIN))
168-
.get();
181+
new AuthenticationRequest(user, password != null ? password : "", ArangoDBConstants.ENCRYPTION_PLAIN),
182+
connection).get();
169183
} catch (final InterruptedException e) {
170184
throw new ArangoDBException(e);
171185
} catch (final ExecutionException e) {

src/test/java/com/arangodb/ArangoCollectionTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void getDocumentIfNoneMatchFail() throws InterruptedException, ExecutionE
227227

228228
@Test
229229
public void getDocumentAsJson() throws InterruptedException, ExecutionException {
230-
db.collection(COLLECTION_NAME).insertDocument("{\"_key\":\"docRaw\",\"a\":\"test\"}", null);
230+
db.collection(COLLECTION_NAME).insertDocument("{\"_key\":\"docRaw\",\"a\":\"test\"}", null).get();
231231
final CompletableFuture<String> f = db.collection(COLLECTION_NAME).getDocument("docRaw", String.class, null);
232232
assertThat(f, is(notNullValue()));
233233
f.whenComplete((readResult, ex) -> {
@@ -1357,7 +1357,7 @@ public void importDocumentsOverwriteTrue() throws InterruptedException, Executio
13571357

13581358
@Test
13591359
public void importDocumentsFromToPrefix() throws InterruptedException, ExecutionException {
1360-
db.createCollection(COLLECTION_NAME + "_edge", new CollectionCreateOptions().type(CollectionType.EDGES));
1360+
db.createCollection(COLLECTION_NAME + "_edge", new CollectionCreateOptions().type(CollectionType.EDGES)).get();
13611361
final ArangoCollectionAsync collection = db.collection(COLLECTION_NAME + "_edge");
13621362
try {
13631363
final Collection<BaseEdgeDocument> values = new ArrayList<>();
@@ -1547,7 +1547,7 @@ public void importDocumentsJsonOverwriteTrue() throws InterruptedException, Exec
15471547

15481548
@Test
15491549
public void importDocumentsJsonFromToPrefix() throws InterruptedException, ExecutionException {
1550-
db.createCollection(COLLECTION_NAME + "_edge", new CollectionCreateOptions().type(CollectionType.EDGES));
1550+
db.createCollection(COLLECTION_NAME + "_edge", new CollectionCreateOptions().type(CollectionType.EDGES)).get();
15511551
final ArangoCollectionAsync collection = db.collection(COLLECTION_NAME + "_edge");
15521552
try {
15531553
final String[] keys = { "1", "2" };
@@ -1586,7 +1586,7 @@ public void deleteDocumentsByKey() throws InterruptedException, ExecutionExcepti
15861586
e.setKey("2");
15871587
values.add(e);
15881588
}
1589-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1589+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
15901590
final Collection<String> keys = new ArrayList<>();
15911591
keys.add("1");
15921592
keys.add("2");
@@ -1617,7 +1617,7 @@ public void deleteDocumentsByDocuments() throws InterruptedException, ExecutionE
16171617
e.setKey("2");
16181618
values.add(e);
16191619
}
1620-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1620+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
16211621
final CompletableFuture<MultiDocumentEntity<DocumentDeleteEntity<Object>>> f = db.collection(COLLECTION_NAME)
16221622
.deleteDocuments(values, null, null);
16231623
assertThat(f, is(notNullValue()));
@@ -1640,7 +1640,7 @@ public void deleteDocumentsByKeyOne() throws InterruptedException, ExecutionExce
16401640
e.setKey("1");
16411641
values.add(e);
16421642
}
1643-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1643+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
16441644
final Collection<String> keys = new ArrayList<>();
16451645
keys.add("1");
16461646
final CompletableFuture<MultiDocumentEntity<DocumentDeleteEntity<Object>>> f = db.collection(COLLECTION_NAME)
@@ -1665,7 +1665,7 @@ public void deleteDocumentsByDocumentOne() throws InterruptedException, Executio
16651665
e.setKey("1");
16661666
values.add(e);
16671667
}
1668-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1668+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
16691669
final CompletableFuture<MultiDocumentEntity<DocumentDeleteEntity<Object>>> f = db.collection(COLLECTION_NAME)
16701670
.deleteDocuments(values, null, null);
16711671
assertThat(f, is(notNullValue()));
@@ -1683,7 +1683,7 @@ public void deleteDocumentsByDocumentOne() throws InterruptedException, Executio
16831683
@Test
16841684
public void deleteDocumentsEmpty() throws InterruptedException, ExecutionException {
16851685
final Collection<BaseDocument> values = new ArrayList<>();
1686-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1686+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
16871687
final Collection<String> keys = new ArrayList<>();
16881688
final CompletableFuture<MultiDocumentEntity<DocumentDeleteEntity<Object>>> f = db.collection(COLLECTION_NAME)
16891689
.deleteDocuments(keys, null, null);
@@ -1699,7 +1699,7 @@ public void deleteDocumentsEmpty() throws InterruptedException, ExecutionExcepti
16991699
@Test
17001700
public void deleteDocumentsByKeyNotExisting() throws InterruptedException, ExecutionException {
17011701
final Collection<BaseDocument> values = new ArrayList<>();
1702-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1702+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
17031703
final Collection<String> keys = new ArrayList<>();
17041704
keys.add("1");
17051705
keys.add("2");
@@ -1775,7 +1775,7 @@ public void updateDocumentsOne() throws InterruptedException, ExecutionException
17751775
e.setKey("1");
17761776
values.add(e);
17771777
}
1778-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1778+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
17791779
final Collection<BaseDocument> updatedValues = new ArrayList<>();
17801780
final BaseDocument first = values.iterator().next();
17811781
first.addAttribute("a", "test");
@@ -1809,7 +1809,7 @@ public void updateDocumentsWithoutKey() throws InterruptedException, ExecutionEx
18091809
{
18101810
values.add(new BaseDocument("1"));
18111811
}
1812-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1812+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
18131813
final Collection<BaseDocument> updatedValues = new ArrayList<>();
18141814
for (final BaseDocument i : values) {
18151815
i.addAttribute("a", "test");
@@ -1833,7 +1833,7 @@ public void replaceDocuments() throws InterruptedException, ExecutionException {
18331833
values.add(new BaseDocument("1"));
18341834
values.add(new BaseDocument("2"));
18351835
}
1836-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1836+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
18371837
final Collection<BaseDocument> updatedValues = new ArrayList<>();
18381838
for (final BaseDocument i : values) {
18391839
i.addAttribute("a", "test");
@@ -1857,7 +1857,7 @@ public void replaceDocumentsOne() throws InterruptedException, ExecutionExceptio
18571857
e.setKey("1");
18581858
values.add(e);
18591859
}
1860-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1860+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
18611861
final Collection<BaseDocument> updatedValues = new ArrayList<>();
18621862
final BaseDocument first = values.iterator().next();
18631863
first.addAttribute("a", "test");
@@ -1891,7 +1891,7 @@ public void replaceDocumentsWithoutKey() throws InterruptedException, ExecutionE
18911891
{
18921892
values.add(new BaseDocument("1"));
18931893
}
1894-
db.collection(COLLECTION_NAME).insertDocuments(values, null);
1894+
db.collection(COLLECTION_NAME).insertDocuments(values, null).get();
18951895
final Collection<BaseDocument> updatedValues = new ArrayList<>();
18961896
for (final BaseDocument i : values) {
18971897
i.addAttribute("a", "test");
@@ -1985,7 +1985,7 @@ public void rename() throws InterruptedException, ExecutionException {
19851985
} catch (final Exception e) {
19861986
}
19871987
} finally {
1988-
db.collection(COLLECTION_NAME + "1").rename(COLLECTION_NAME);
1988+
db.collection(COLLECTION_NAME + "1").rename(COLLECTION_NAME).get();
19891989
}
19901990
}
19911991

src/test/java/com/arangodb/ArangoDBTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void getDatabases() throws InterruptedException, ExecutionException {
114114
assertThat(dbs.size(), is(greaterThan(0)));
115115
final int dbCount = dbs.size();
116116
assertThat(dbs.iterator().next(), is("_system"));
117-
arangoDB.createDatabase(BaseTest.TEST_DB);
117+
arangoDB.createDatabase(BaseTest.TEST_DB).get();
118118
dbs = arangoDB.getDatabases().get();
119119
assertThat(dbs.size(), is(greaterThan(dbCount)));
120120
assertThat(dbs, hasItem("_system"));

src/test/java/com/arangodb/ArangoDatabaseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void deleteSystemCollectionFail() throws InterruptedException, ExecutionE
139139
@Test
140140
public void getIndex() throws InterruptedException, ExecutionException {
141141
try {
142-
db.createCollection(COLLECTION_NAME, null);
142+
db.createCollection(COLLECTION_NAME, null).get();
143143
final Collection<String> fields = new ArrayList<>();
144144
fields.add("a");
145145
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null).get();
@@ -158,7 +158,7 @@ public void getIndex() throws InterruptedException, ExecutionException {
158158
@Test
159159
public void deleteIndex() throws InterruptedException, ExecutionException {
160160
try {
161-
db.createCollection(COLLECTION_NAME, null);
161+
db.createCollection(COLLECTION_NAME, null).get();
162162
final Collection<String> fields = new ArrayList<>();
163163
fields.add("a");
164164
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null).get();

0 commit comments

Comments
 (0)