Skip to content

Commit afdcf5d

Browse files
committed
Removed Connection.open. Implementations should open the connection in the constructor.
1 parent e2b285c commit afdcf5d

16 files changed

Lines changed: 31 additions & 140 deletions

driver/src/main/org/mongodb/connection/Connection.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
@NotThreadSafe
3434
public interface Connection extends BaseConnection {
3535

36-
/**
37-
* Open the connection. This method can be called multiple times, and all but the first should be a no-op.
38-
*/
39-
void open();
40-
4136
/**
4237
* Send a message to the server. The connection may not make any attempt to validate the integrity of the message.
4338
* <p>

driver/src/main/org/mongodb/connection/impl/AuthenticatingConnection.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public AuthenticatingConnection(final Connection wrapped, final List<MongoCreden
4343

4444
notNull("credentialList", credentialList);
4545
this.credentialList = new ArrayList<MongoCredential>(credentialList);
46+
authenticateAll();
4647
}
4748

4849
@Override
@@ -60,26 +61,19 @@ public boolean isClosed() {
6061

6162
@Override
6263
public ServerAddress getServerAddress() {
63-
isTrue("open", wrapped != null);
64-
return wrapped.getServerAddress();
65-
}
66-
67-
@Override
68-
public void open() {
6964
isTrue("open", !isClosed());
70-
wrapped.open();
71-
authenticateAll();
65+
return wrapped.getServerAddress();
7266
}
7367

7468
@Override
7569
public void sendMessage(final List<ByteBuf> byteBuffers) {
76-
isTrue("open", wrapped != null);
70+
isTrue("open", !isClosed());
7771
wrapped.sendMessage(byteBuffers);
7872
}
7973

8074
@Override
8175
public ResponseBuffers receiveMessage(final ResponseSettings responseSettings) {
82-
isTrue("open", wrapped != null);
76+
isTrue("open", !isClosed());
8377
return wrapped.receiveMessage(responseSettings);
8478
}
8579

driver/src/main/org/mongodb/connection/impl/ConnectingServerConnection.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public ServerDescription getDescription() {
4545
return serverDescription;
4646
}
4747

48-
@Override
49-
public void open() {
50-
connection.open();
51-
}
52-
5348
@Override
5449
public void sendMessage(final List<ByteBuf> byteBuffers) {
5550
connection.sendMessage(byteBuffers);

driver/src/main/org/mongodb/connection/impl/DefaultConnection.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ public ServerAddress getServerAddress() {
6767
return serverAddress;
6868
}
6969

70-
@Override
71-
public void open() {
72-
isTrue("open", !isClosed());
73-
ensureOpen();
74-
}
75-
7670
public void sendMessage(final List<ByteBuf> byteBuffers) {
7771
isTrue("open", !isClosed());
7872
try {
@@ -100,8 +94,6 @@ public ResponseBuffers receiveMessage(final ResponseSettings responseSettings) {
10094
}
10195
}
10296

103-
protected abstract void ensureOpen();
104-
10597
protected abstract void write(final List<ByteBuf> buffers) throws IOException;
10698

10799
protected abstract void read(final ByteBuf buffer) throws IOException;
@@ -152,7 +144,7 @@ private ResponseBuffers receiveMessage(final ResponseSettings responseSettings,
152144
return new ResponseBuffers(replyHeader, bodyByteBuffer, System.nanoTime() - start);
153145
}
154146

155-
protected void initializeSocket(final Socket socket) throws IOException {
147+
protected final void initializeSocket(final Socket socket) throws IOException {
156148
socket.setTcpNoDelay(true);
157149
socket.setSoTimeout(settings.getReadTimeoutMS());
158150
socket.setKeepAlive(settings.isKeepAlive());

driver/src/main/org/mongodb/connection/impl/DefaultConnectionProvider.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public DefaultConnectionProvider(final ServerAddress serverAddress, final Connec
4747
new ConcurrentPool.ItemFactory<UsageTrackingConnection>() {
4848
@Override
4949
public UsageTrackingConnection create() {
50-
UsageTrackingConnection connection = new UsageTrackingConnection(connectionFactory.create(serverAddress), generation.get());
51-
connection.open();
52-
return connection;
50+
return new UsageTrackingConnection(connectionFactory.create(serverAddress), generation.get());
5351
}
5452

5553
@Override
@@ -136,12 +134,6 @@ public ServerAddress getServerAddress() {
136134
return wrapped.getServerAddress();
137135
}
138136

139-
@Override
140-
public void open() {
141-
isTrue("open", !isClosed());
142-
wrapped.open();
143-
}
144-
145137
@Override
146138
public void sendMessage(final List<ByteBuf> byteBuffers) {
147139
isTrue("open", wrapped != null);

driver/src/main/org/mongodb/connection/impl/DefaultServer.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,6 @@ public ServerAddress getServerAddress() {
167167
return wrapped.getServerAddress();
168168
}
169169

170-
@Override
171-
public void open() {
172-
isTrue("open", !isClosed());
173-
try {
174-
wrapped.open();
175-
} catch (MongoException e) {
176-
handleException();
177-
throw e;
178-
}
179-
}
180-
181170
@Override
182171
public void sendMessage(final List<ByteBuf> byteBuffers) {
183172
isTrue("open", !isClosed());

driver/src/main/org/mongodb/connection/impl/DefaultSocketChannelConnection.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,14 @@
2929

3030
// TODO: migrate all the DBPort configuration
3131
class DefaultSocketChannelConnection extends DefaultConnection {
32-
private volatile SocketChannel socketChannel;
32+
private final SocketChannel socketChannel;
3333

3434
public DefaultSocketChannelConnection(final ServerAddress address, final DefaultConnectionSettings settings,
3535
final BufferProvider bufferProvider) {
3636
super(address, settings, bufferProvider);
37-
}
38-
39-
protected void ensureOpen() {
4037
try {
41-
if (socketChannel == null) {
42-
socketChannel = SocketChannel.open();
43-
initializeSocket(socketChannel.socket());
44-
}
38+
socketChannel = SocketChannel.open();
39+
initializeSocket(socketChannel.socket());
4540
} catch (IOException e) {
4641
close();
4742
throw new MongoSocketOpenException("Exception opening socket", getServerAddress(), e);
@@ -79,10 +74,9 @@ public void close() {
7974
try {
8075
if (socketChannel != null) {
8176
socketChannel.close();
82-
socketChannel = null;
83-
super.close();
8477
}
85-
} catch (IOException e) { //NOPMD
78+
super.close();
79+
} catch (IOException e) {
8680
// ignore
8781
}
8882
}

driver/src/main/org/mongodb/connection/impl/DefaultSocketConnection.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,14 @@
2929

3030
// TODO: migrate all the DBPort configuration
3131
class DefaultSocketConnection extends DefaultConnection {
32-
private final SocketFactory socketFactory;
33-
private volatile Socket socket;
32+
private final Socket socket;
3433

3534
public DefaultSocketConnection(final ServerAddress address, final DefaultConnectionSettings settings,
3635
final BufferProvider bufferProvider, final SocketFactory socketFactory) {
3736
super(address, settings, bufferProvider);
38-
this.socketFactory = socketFactory;
39-
}
40-
41-
protected void ensureOpen() {
4237
try {
43-
if (socket == null) {
44-
socket = socketFactory.createSocket();
45-
initializeSocket(socket);
46-
}
38+
socket = socketFactory.createSocket();
39+
initializeSocket(socket);
4740
} catch (IOException e) {
4841
close();
4942
throw new MongoSocketOpenException("Exception opening socket", getServerAddress(), e);
@@ -69,15 +62,13 @@ protected void read(final ByteBuf buffer) throws IOException {
6962
}
7063
}
7164

72-
//CHECKSTYLE:OFF
7365
public void close() {
7466
try {
75-
super.close();
7667
if (socket != null) {
7768
socket.close();
78-
socket = null;
7969
}
80-
} catch (IOException e) { // NOPMD
70+
super.close();
71+
} catch (IOException e) {
8172
// ignore
8273
}
8374
}

driver/src/main/org/mongodb/connection/impl/IsMasterServerStateNotifier.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ public synchronized void run() {
9191
try {
9292
if (connection == null) {
9393
connection = connectionFactory.create(serverAddress);
94-
connection.open();
9594
}
9695
try {
9796
final CommandResult commandResult = new CommandOperation("admin",

driver/src/main/org/mongodb/connection/impl/UsageTrackingConnection.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@
3030
* A connection that tracks when it was opened and when it was last used.
3131
*/
3232
class UsageTrackingConnection implements Connection {
33-
private volatile long openedAt = Long.MAX_VALUE;
34-
private volatile long lastUsedAt = Long.MAX_VALUE;
33+
private final long openedAt;
34+
private volatile long lastUsedAt;
3535
private final int generation;
3636
private volatile Connection wrapped;
37-
private volatile boolean isOpen;
3837

3938
UsageTrackingConnection(final Connection wrapped, final int generation) {
4039
this.wrapped = wrapped;
4140
this.generation = generation;
41+
openedAt = System.currentTimeMillis();
42+
lastUsedAt = openedAt;
4243
}
4344

4445
@Override
@@ -58,16 +59,6 @@ public ServerAddress getServerAddress() {
5859
return wrapped.getServerAddress();
5960
}
6061

61-
@Override
62-
public void open() {
63-
if (!isOpen) {
64-
wrapped.open();
65-
isOpen = true;
66-
openedAt = System.currentTimeMillis();
67-
lastUsedAt = openedAt;
68-
}
69-
}
70-
7162
@Override
7263
public void sendMessage(final List<ByteBuf> byteBuffers) {
7364
wrapped.sendMessage(byteBuffers);
@@ -83,6 +74,7 @@ public ResponseBuffers receiveMessage(final ResponseSettings responseSettings) {
8374

8475
/**
8576
* Gets the generation of this connection. This can be used by connection pools to track whether the connection is stale.
77+
*
8678
* @return the generation.
8779
*/
8880
int getGeneration() {
@@ -91,6 +83,7 @@ int getGeneration() {
9183

9284
/**
9385
* Returns the time at which this connection was opened, or {@code Long.MAX_VALUE} if it has not yet been opened.
86+
*
9487
* @return the time when this connection was opened, in milliseconds since the epoch.
9588
*/
9689
long getOpenedAt() {
@@ -99,6 +92,7 @@ long getOpenedAt() {
9992

10093
/**
10194
* Returns the time at which this connection was last used, or {@code Long.MAX_VALUE} if it has not yet been used.
95+
*
10296
* @return the time when this connection was last used, in milliseconds since the epoch.
10397
*/
10498
long getLastUsedAt() {

0 commit comments

Comments
 (0)