首页 > 代码库 > Linux中Hibernate连接不上MySql,但是在外网都能访问

Linux中Hibernate连接不上MySql,但是在外网都能访问

题描述

在Linux上使用Spring+Hibernate+dbcp连接本机上的MySQL时,出现异常:

[ERROR][2014-09-1614:00:59,343][com.ocyd.jeecgframework.core.common.exception.MyExceptionHandler]org.springframework.transaction.CannotCreateTransactionException:Could not open Hibernate Session for transaction; nested exception isorg.hibernate.exception.GenericJDBCException: Could not open connection

         atorg.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:440)

         atorg.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)

         atorg.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)

         atorg.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)

         atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

         at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

         at$Proxy27.checkUserExits(Unknown Source)

         atcom.ocyd.appfactory.controller.LoginController.checkuser(LoginController.java:78)

         atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

         atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

         atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

         atjava.lang.reflect.Method.invoke(Method.java:597)

         at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)

         atorg.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)

         at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)

         atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)

         atorg.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)

         atorg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)

         atorg.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)

         at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)

         atjavax.servlet.http.HttpServlet.service(HttpServlet.java:723)

         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

         atcom.ocyd.jeecgframework.core.aop.GZipFilter.doFilter(GZipFilter.java:51)

         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

         atorg.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)

         atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

         atorg.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:119)

         atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

         atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)

         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

         atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

         atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)

         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

         atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)

         atorg.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)

         atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)

         atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)

         atjava.lang.Thread.run(Thread.java:619)

Caused by: org.hibernate.exception.GenericJDBCException: Could notopen connection

         atorg.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)

         atorg.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)

         atorg.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)

         atorg.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)

         atorg.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)

         atorg.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)

         atorg.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)

         atorg.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1309)

         atorg.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:399)

         ... 43 more

Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot createPoolableConnectionFactory (Could not create connection to database server.)

         atorg.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)

         atorg.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)

         atorg.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)

         atorg.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)

         atorg.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:277)

         atorg.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)

         ... 48 more

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:Could not create connection to database server.

         atsun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

         atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

         atsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

         atjava.lang.reflect.Constructor.newInstance(Constructor.java:513)

         atcom.mysql.jdbc.Util.handleNewInstance(Util.java:411)

         at com.mysql.jdbc.Util.getInstance(Util.java:386)

         atcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)

         atcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)

         atcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)

         at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)

         atcom.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2411)

         atcom.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)

         atcom.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)

         atcom.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)

         atsun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

         atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

         atjava.lang.reflect.Constructor.newInstance(Constructor.java:513)

         atcom.mysql.jdbc.Util.handleNewInstance(Util.java:411)

         atcom.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)

         atcom.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)

         atorg.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)

         atorg.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)

         atorg.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)

         atorg.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)

         ... 53 more

Caused by: java.lang.NullPointerException

         atjava.security.MessageDigest.update(MessageDigest.java:293)

         atjava.security.MessageDigest.digest(MessageDigest.java:368)

         atcom.mysql.jdbc.Security.scramble411(Security.java:329)

         atcom.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4087)

         atcom.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)

         atcom.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)

         atcom.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)

         ... 67 more

 

因为部署在其他机器上时,可以正常访问该Linux上的MySQL,所以首先检查MySQL中的user表,排除了访问权限的问题。

在网上搜索,发现有个类似的问题:http://www.oschina.net/question/66562_114082?sort=time,但他的原因是“在64位操作系统下使用了32位的操作系统导致的”。检查Linux和使用的Java都是32位的。

后来查看MySQLDriver的源代码,发现出错的地方(Security.java:329),是在对密码进行加密的地方,其中有从字符串中获取byte[]的操作,其中涉及到charset,怀疑是Java1.6(Linux版本)中对中文字符的支持问题。写了一段测试代码,发现使用GBK字符集时,Java1.6会报错。

下载了Java1.7,重新设置路径,启动tomcat,问题解决。

Linux中Hibernate连接不上MySql,但是在外网都能访问