4545import java .sql .Types ;
4646import java .util .ArrayList ;
4747import java .util .List ;
48-
48+ import java . util . concurrent . atomic . AtomicBoolean ;
4949
5050public class DatabasesPollingProtocolAdapter implements BatchPollingProtocolAdapter {
5151
5252 public static final int TIMEOUT = 30 ;
5353 private static final @ NotNull Logger log = LoggerFactory .getLogger (DatabasesPollingProtocolAdapter .class );
5454 private static final @ NotNull ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
55+
5556 private final @ NotNull DatabasesAdapterConfig adapterConfig ;
5657 private final @ NotNull ProtocolAdapterInformation adapterInformation ;
5758 private final @ NotNull ProtocolAdapterState protocolAdapterState ;
5859 private final @ NotNull String adapterId ;
5960 private final @ NotNull List <Tag > tags ;
6061 private final @ NotNull DatabaseConnection databaseConnection ;
6162 private final @ NotNull AdapterFactories adapterFactories ;
63+ private final @ NotNull AtomicBoolean started ;
6264
6365 public DatabasesPollingProtocolAdapter (
6466 final @ NotNull ProtocolAdapterInformation adapterInformation ,
@@ -69,9 +71,8 @@ public DatabasesPollingProtocolAdapter(
6971 this .protocolAdapterState = input .getProtocolAdapterState ();
7072 this .tags = input .getTags ();
7173 this .adapterFactories = input .adapterFactories ();
72-
74+ this . started = new AtomicBoolean ( false );
7375 log .debug ("Building connection string" );
74-
7576 this .databaseConnection = new DatabaseConnection (adapterConfig .getType (),
7677 adapterConfig .getServer (),
7778 adapterConfig .getPort (),
@@ -91,53 +92,74 @@ public DatabasesPollingProtocolAdapter(
9192 public void start (
9293 final @ NotNull ProtocolAdapterStartInput input ,
9394 final @ NotNull ProtocolAdapterStartOutput output ) {
94- log .debug ("Loading PostgreSQL Driver" );
95- try {
96- Class .forName ("org.postgresql.Driver" );
97- } catch (final ClassNotFoundException e ) {
98- output .failStart (e , null );
95+ if (!started .compareAndSet (false , true )) {
96+ log .debug ("Database adapter {} already started, returning success" , adapterId );
97+ output .startedSuccessfully ();
9998 return ;
10099 }
101100
102- log .debug ("Loading MariaDB Driver (for MySQL)" );
103101 try {
104- Class .forName ("org.mariadb.jdbc.Driver" );
105- } catch (final ClassNotFoundException e ) {
106- output .failStart (e , null );
107- return ;
108- }
102+ log .debug ("Loading PostgreSQL Driver" );
103+ try {
104+ Class .forName ("org.postgresql.Driver" );
105+ } catch (final ClassNotFoundException e ) {
106+ output .failStart (e , null );
107+ started .set (false );
108+ return ;
109+ }
109110
110- log .debug ("Loading MS SQL Driver" );
111- try {
112- Class .forName ("com.microsoft.sqlserver.jdbc.SQLServerDataSource" );
113- } catch (final ClassNotFoundException e ) {
114- output .failStart (e , null );
115- return ;
116- }
111+ log .debug ("Loading MariaDB Driver (for MySQL)" );
112+ try {
113+ Class .forName ("org.mariadb.jdbc.Driver" );
114+ } catch (final ClassNotFoundException e ) {
115+ output .failStart (e , null );
116+ started .set (false );
117+ return ;
118+ }
117119
120+ log .debug ("Loading MS SQL Driver" );
121+ try {
122+ Class .forName ("com.microsoft.sqlserver.jdbc.SQLServerDataSource" );
123+ } catch (final ClassNotFoundException e ) {
124+ output .failStart (e , null );
125+ started .set (false );
126+ return ;
127+ }
118128
119- databaseConnection .connect ();
129+ databaseConnection .connect ();
120130
121- try {
122- log .debug ("Starting connection to the database instance" );
123- if (databaseConnection .getConnection ().isValid (TIMEOUT )) {
124- output .startedSuccessfully ();
125- protocolAdapterState .setConnectionStatus (ProtocolAdapterState .ConnectionStatus .CONNECTED );
126- } else {
127- output .failStart (new Throwable ("Error connecting database, please check the configuration" ),
128- "Error connecting database, please check the configuration" );
131+ try {
132+ log .debug ("Starting connection to the database instance" );
133+ if (databaseConnection .getConnection ().isValid (TIMEOUT )) {
134+ output .startedSuccessfully ();
135+ protocolAdapterState .setConnectionStatus (ProtocolAdapterState .ConnectionStatus .CONNECTED );
136+ } else {
137+ output .failStart (new Throwable ("Error connecting database, please check the configuration" ),
138+ "Error connecting database, please check the configuration" );
139+ protocolAdapterState .setConnectionStatus (ProtocolAdapterState .ConnectionStatus .DISCONNECTED );
140+ started .set (false );
141+ }
142+ } catch (final Exception e ) {
143+ output .failStart (e , null );
129144 protocolAdapterState .setConnectionStatus (ProtocolAdapterState .ConnectionStatus .DISCONNECTED );
145+ started .set (false );
130146 }
131147 } catch (final Exception e ) {
148+ log .error ("Unexpected error during adapter start" , e );
132149 output .failStart (e , null );
133- protocolAdapterState . setConnectionStatus ( ProtocolAdapterState . ConnectionStatus . DISCONNECTED );
150+ started . set ( false );
134151 }
135152 }
136153
137154 @ Override
138155 public void stop (
139156 final @ NotNull ProtocolAdapterStopInput protocolAdapterStopInput ,
140157 final @ NotNull ProtocolAdapterStopOutput protocolAdapterStopOutput ) {
158+ if (!started .compareAndSet (true , false )) {
159+ log .debug ("Database adapter {} already stopped, returning success" , adapterId );
160+ protocolAdapterStopOutput .stoppedSuccessfully ();
161+ return ;
162+ }
141163 databaseConnection .close ();
142164 protocolAdapterStopOutput .stoppedSuccessfully ();
143165 }
@@ -247,5 +269,4 @@ public int getPollingIntervalMillis() {
247269 public int getMaxPollingErrorsBeforeRemoval () {
248270 return adapterConfig .getMaxPollingErrorsBeforeRemoval ();
249271 }
250-
251272}
0 commit comments