用java -jar启动之后,只要一访问数据库就报错,错误信息如下:
文字如下:
2019-07-02 10:25:12.220 ---> [http-nio-8080-exec-1] ---> ERROR com.alibaba.druid.pool.DruidDataSource - {dataSource-1} init error
java.sql.SQLException: com.mysql.cj.jdbc.Driver
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:620)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:885)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1311)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1307)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:109)
at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:82)
at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:68)
at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:336)
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:84)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
at com.sun.proxy.$Proxy99.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy102.selectAll(Unknown Source)
at com.shopin.handtopgrade.service.Impl.DescChannelTypeServiceImpl.selectAll(DescChannelTypeServiceImpl.java:34)
at com.shopin.handtopgrade.controller.DescChannelTypeController.selectAll(DescChannelTypeController.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
折腾了一天时间,最后终于发现问题,并解决了。
现象:
在本地访问数据库没有问题,但是一发布到服务器,只要一链接数据库就报错。
解决路径:
1、一开始我以为是数据库限制了这台服务器的访问,结果在服务器装上mysql客户端,是可以成功链接mysql的,这预言错误;
2、我项目中有application.yml和application.properties两个配置,朋友说有可能是冲突了,让我删一个,我把application.properties删掉,结果还是不行,所以这个预言也不立;
3、我仔细看了下pom文件,发现pom中 mysql-connector-java 的版本,与External Libraries包中的版本号不一样,我想可能是版本问题,于是把pom中的版本号删掉,奇迹出现了,结果问题解决了。