线程“主要” org.springframework.jdbc.CannotGetJdbcConnectionException中的异常

时间:2019-02-04 07:48:36

标签: java spring spring-boot spring-mvc

请帮助我解决这个错误,我是春季新手。因此,当我尝试在没有Spring MVC网络结构的情况下运行测试数据源时,我无法理解此错误

cd F:\NetBeansProjects\SpringContactApp; "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_131" cmd /c "\"\"C:\\Program Files\\NetBeans 8.0.1\\java\\maven\\bin\\mvn.bat\" -Dexec.args=\"-classpath %classpath in.ezeon.capp.test.TestDataSource\" -Dexec.executable=java -Dexec.classpathScope=test -Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans 8.0.1\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:1.2.1:exec\""
        Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
        Scanning for projects...

        ------------------------------------------------------------------------
        Building SpringContactApp 1.0-SNAPSHOT
        ------------------------------------------------------------------------

        --- exec-maven-plugin:1.2.1:exec (default-cli) @ SpringContactApp ---
        Feb 04, 2019 12:15:40 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
        INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@531d72ca: startup date [Mon Feb 04 12:15:40 IST 2019]; root of context hierarchy
        Feb 04, 2019 12:15:40 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
        INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
        Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)
            at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
            at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:619)
            at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:870)
            at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:931)
            at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:941)
            at in.ezeon.capp.test.TestDataSource.main(TestDataSource.java:23)
        Caused by: java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)
            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 org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
            at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
            ... 5 more
        Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

下面的代码是SpringRootConfig.java类和TestDataSource.java类 而且我正在使用netbeans 8和spring框架。

package in.ezeon.capp.config;

import static java.lang.Boolean.TRUE;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 *
 * @author admin
 */
@Configuration
@ComponentScan(basePackages = {"in.ezeon.capp.dao"})
public class SpringRootConfig {
    //TODO: Services,DAO,DataSource,Email sender or some other business layer beans
    @Bean
    public BasicDataSource getDataSource()
    {
        BasicDataSource ds=new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        //ds.setUrl("jdbc:mysql://localhost:3306/capp_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false");//localhost//127.0.0.1
        ds.setUrl("jdbc:mysql://localhost:3306/capp_db");
        ds.setUsername("root");
        ds.setPassword("dbadmin@123");//dbadmin@123
        ds.setMaxTotal(2);
        ds.setInitialSize(1);
        ds.setTestOnBorrow(true);
        ds.setValidationQuery("SELECT 1");
        ds.setDefaultAutoCommit(TRUE);
        return ds;
    }
}

并找到下面的屏幕快照以查看该错误。

enter image description here

2 个答案:

答案 0 :(得分:0)

该错误与数据库连接有关。立即检查您正在使用的数据。可能不是以与您连接的相同名称创建的数据库。

答案 1 :(得分:0)

在您的情况下:

 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
 Could not create connection to database server. Attempted reconnect 3 times. Giving up.
  • 检查您的jdbc网址。
  • 检查数据库的可用性(在该url上,运行服务器?端口?数据库?理想情况下是同一驱动程序,同一网络。例如,使用Netbeans“服务”选项卡>“数据库”)
  • 检查网络/代理/防火墙,您在其中。