title: How can I fix the error: java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)?
body: <p>I'm using Maven project with JSP + Servlets.</p>
  <p>In <em>Main</em> class I'm connecting via:</p>
  <pre><code>Connection connection = ConnectionFactory.getInstance().getConnection();
  </code></pre>
  <p>And logic for <em>ConnectionFactory</em> class is:</p>
  <pre><code>    public class ConnectionFactory {
      
          static final Logger logger = Logger.getLogger(ConnectionFactory.class);
      
          private static final String PATH_TO_PROPERTIES = RootPathUtil.getRootApplicationClassPath(&quot;database.properties&quot;);
          private static final Properties properties;
          
          static {
              InputStream inputStream;
              Properties prop = new Properties();
              try {
                  inputStream = new FileInputStream(PATH_TO_PROPERTIES);
                  prop.load(inputStream);
                  logger.info(&quot;Connection Factory Property loaded.\n\t\tProperty file location: &quot; + PATH_TO_PROPERTIES);
              } catch (FileNotFoundException e) {
                  logger.error(&quot;Property file \&quot;database.properties\&quot; didn't found. Database main properties didn't initialize.&quot;);
                  e.printStackTrace();
              } catch (IOException e) {
                  logger.error(&quot;IO exception during property file \&quot;database.properties\&quot; loading. Database main properties didn't initialize.&quot;);
                  e.printStackTrace();
              }
              properties = prop;
          }
      
          private static ConnectionFactory instance = new ConnectionFactory();
          private BasicDataSource ds;
      
          private ConnectionFactory() {
              if (properties.isEmpty()){
                  logger.error(&quot;Property instance is empty. Database main properties didn't initialize. ConnectionFactory creation failed.&quot;);
                  throw new PropertyLoadException(&quot;ConnectionFactory creation failed. Property didn't load.&quot;);
              }
              ds = new BasicDataSource();
              ds.setDriverClassName(properties.getProperty(&quot;dbDriver&quot;));
              ds.setUsername(properties.getProperty(&quot;userName&quot;));
              ds.setPassword(properties.getProperty(&quot;password&quot;));
              ds.setUrl(properties.getProperty(&quot;connectionUrl&quot;));
      
              ds.setMinIdle(Integer.parseInt(properties.getProperty(&quot;connection_pull.min_idle&quot;)));
              ds.setMaxIdle(Integer.parseInt(properties.getProperty(&quot;connection_pull.max_idle&quot;)));
              ds.setMaxOpenPreparedStatements(Integer.parseInt(properties.getProperty(&quot;connection_pull.max_open_prepared_statements&quot;)));
          }
      
          public static ConnectionFactory getInstance() {
              if (instance == null) {
                  instance = new ConnectionFactory();
                  return instance;
              } else {
                  return instance;
              }
          }
      
          public Connection getConnection() throws SQLException {
              return this.ds.getConnection();
          }
      }
  </code></pre>
  <p>When I'm running Tomcat Server, I'm always getting the issue like:</p>
  <pre><code>java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)
      at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2294)
      at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2039)
      at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1533)
      at com.test.project.carrental.data.service.ConnectionFactory.getConnection(ConnectionFactory.java:69)
      at com.test.project.carrental.data.service.DataBaseUtil.getConnection(DataBaseUtil.java:21)
      at com.test.project.carrental.controller.filter.JDBCFilter.doFilter(JDBCFilter.java:63)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at com.test.project.carrental.controller.filter.LogSessionIdFilter.doFilter(LogSessionIdFilter.java:39)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
      at com.mysql.jdbc.Util.getInstance(Util.java:387)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2330)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:806)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
      at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:39)
      at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:256)
      at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2304)
      at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2290)
      ... 27 more
  Caused by: java.lang.NullPointerException
      at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2997)
      at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1936)
      at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1865)
      at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1228)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
      ... 41 more
  </code></pre>
  <p>To solve this problem, I tried different versions for <code>mysql-connector-java</code> dependency like:</p>
  <pre><code>        &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;8.0.20&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  <p>Also, as mentioned <a href="https://stackoverflow.com/a/50082287/8370915">here</a> and <a href="https://stackoverflow.com/a/50383070/8370915">here</a>, I tried to use:</p>
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;version&gt;8.0.11&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  <p>Alternative fix using <code>5.1.41</code> or <a href="https://stackoverflow.com/a/50758438/8370915">5.1.46</a> wasn't helpful for me as well.</p>
  <p>At the same time, I tried to use various connection properties for:</p>
  <pre><code>dbDriver=com.mysql.jdbc.Driver
  connectionUrl=jdbc:mysql://localhost:3306/db?verifyServerCertificate=false&amp;useSSL=true
  userName=...
  password=...
  connection_pull.min_idle=5
  connection_pull.max_idle=20
  connection_pull.max_open_prepared_statements=180
  </code></pre>
  <p>I've tested different variants for <code>coonectionUrl</code> like:</p>
  <pre><code>#connectionUrl=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC
  #connectionUrl=jdbc:mysql://localhost:3306/db?autoReconnect=true
  #connectionUrl=jdbc:mysql://localhost:3306/db?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC
  #connectionUrl=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC
  #connectionUrl=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false
  #connectionUrl=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&amp;serverTimezone=UTC&amp;useSSL=false
  </code></pre>
  <p>some connections were based on <a href="https://stackoverflow.com/a/49992175/8370915">this</a> &amp; <a href="https://ru.stackoverflow.com/a/1013113/260756">this</a> &amp; <a href="https://ru.stackoverflow.com/a/615622/260756">this</a> answer.</p>
  <p><strong>UPD:</strong></p>
  <p>I've added my POM file, because, as mentioned in comments below, MySQL Connector/J <code>8.0.11</code> contains both variants of connection strings.</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
           xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
           xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;CarRental&lt;/groupId&gt;
      &lt;artifactId&gt;CarRental&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;name&gt;Car Rental Service&lt;/name&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;junit.version&gt;4.12&lt;/junit.version&gt;
          &lt;mokito.version&gt;1.10.19&lt;/mokito.version&gt;
          &lt;slf4j.version&gt;1.7.21&lt;/slf4j.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;!-- SERVLETS AND VIEWS--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
              &lt;version&gt;3.1.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
              &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
              &lt;version&gt;2.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- DATDBASE DRIVER --&gt;
          &lt;!--&lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;5.1.39&lt;/version&gt;
          &lt;/dependency&gt;--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;8.0.11&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-dbcp2&lt;/artifactId&gt;
              &lt;version&gt;2.1.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- TESTING --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;${junit.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.mockito&lt;/groupId&gt;
              &lt;artifactId&gt;mockito-all&lt;/artifactId&gt;
              &lt;version&gt;${mokito.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- LOGGING --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
              &lt;version&gt;${slf4j.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
              &lt;version&gt;${slf4j.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;finalName&gt;CarRentalService&lt;/finalName&gt;
  
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.8.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;encoding&gt;UTF-8&lt;/encoding&gt;
                      &lt;source&gt;${java.version}&lt;/source&gt;
                      &lt;target&gt;${java.version}&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;encoding&gt;UTF-8&lt;/encoding&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.0&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.jacoco&lt;/groupId&gt;
                  &lt;artifactId&gt;jacoco-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;0.7.7.201606060606&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;destFile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/destFile&gt;
                      &lt;dataFile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/dataFile&gt;
                  &lt;/configuration&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;jacoco-initialize&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;prepare-agent&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                      &lt;execution&gt;
                          &lt;id&gt;jacoco-site&lt;/id&gt;
                          &lt;phase&gt;package&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;report&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                    &lt;/executions&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.19.1&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;systemPropertyVariables&gt;
                      &lt;jacoco-agent.destfile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/jacoco-agent.destfile&gt;
                  &lt;/systemPropertyVariables&gt;
              &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  <p>I've tried to run <a href="https://stackoverflow.com/a/27270733/8370915">without Intelij IDEA</a> war file - the same issue.</p>