Elffuss BraidHub

kikocis/java-driver public

Ensure channel gets closed when Connection construction fails.
Changeset0f17d5b313f160e1fce7c95ce496cd58362d9492998fdfa54a1a939665dba07b
Authorolim7t <omichallat+github@gmail.com> · 2014-12-18 11:42 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2014-12-19 00:16 +01:00
Parents47df60f15f75

Browse the files at this changeset

1 file changed

modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +39 −22
@@ -84,32 +84,49 @@
8484 this.factory = factory;
8585 this.name = name;
8686
87- ClientBootstrap bootstrap = factory.newBootstrap();
88- ProtocolOptions protocolOptions = factory.configuration.getProtocolOptions();
89- int protocolVersion = factory.protocolVersion == 1 ? 1 : 2;
90- bootstrap.setPipelineFactory(new PipelineFactory(this, protocolVersion, protocolOptions.getCompression().compressor, protocolOptions.getSSLOptions()));
87+ try {
88+ ClientBootstrap bootstrap = factory.newBootstrap();
89+ ProtocolOptions protocolOptions = factory.configuration.getProtocolOptions();
90+ int protocolVersion = factory.protocolVersion == 1 ? 1 : 2;
91+ bootstrap.setPipelineFactory(new PipelineFactory(this, protocolVersion, protocolOptions.getCompression().compressor, protocolOptions.getSSLOptions()));
9192
92- ChannelFuture future = bootstrap.connect(address);
93+ ChannelFuture future = bootstrap.connect(address);
9394
94- writer.incrementAndGet();
95- try {
96- // Wait until the connection attempt succeeds or fails.
97- this.channel = future.awaitUninterruptibly().getChannel();
98- this.factory.allChannels.add(this.channel);
99- if (!future.isSuccess())
100- {
101- if (logger.isDebugEnabled())
102- logger.debug(String.format("%s Error connecting to %s%s", this, address, extractMessage(future.getCause())));
103- throw defunct(new TransportException(address, "Cannot connect", future.getCause()));
95+ writer.incrementAndGet();
96+ try {
97+ // Wait until the connection attempt succeeds or fails.
98+ this.channel = future.awaitUninterruptibly().getChannel();
99+ this.factory.allChannels.add(this.channel);
100+ if (!future.isSuccess()) {
101+ if (logger.isDebugEnabled())
102+ logger.debug(String.format("%s Error connecting to %s%s", this, address, extractMessage(future.getCause())));
103+ throw defunct(new TransportException(address, "Cannot connect", future.getCause()));
104+ }
105+ } finally {
106+ writer.decrementAndGet();
104107 }
105- } finally {
106- writer.decrementAndGet();
107- }
108108
109- logger.trace("{} Connection opened successfully", this);
110- initializeTransport(protocolVersion, factory.manager.metadata.clusterName);
111- logger.debug("{} Transport initialized and ready", this);
112- isInitialized = true;
109+ logger.trace("{} Connection opened successfully", this);
110+ initializeTransport(protocolVersion, factory.manager.metadata.clusterName);
111+ logger.debug("{} Transport initialized and ready", this);
112+ isInitialized = true;
113+
114+ } catch (ConnectionException e) {
115+ closeAsync().force();
116+ throw e;
117+ } catch (ClusterNameMismatchException e) {
118+ closeAsync().force();
119+ throw e;
120+ } catch (UnsupportedProtocolVersionException e) {
121+ closeAsync().force();
122+ throw e;
123+ } catch (InterruptedException e) {
124+ closeAsync().force();
125+ throw e;
126+ } catch (RuntimeException e) {
127+ closeAsync().force();
128+ throw e;
129+ }
113130 }
114131
115132 private static String extractMessage(Throwable t) {