一尘不染

亚马逊aws ubuntu Java mysql web服务“错误调用Driver#connect”

tomcat

我们在tomcat 7 / java7 / hibernate / mysql5上运行了一些Web服务。一切都在我的PC上的localhost
tomcat服务器上完美运行,但是当我们在AWS Ubuntu服务器上部署应用程序时,出现以下错误消息:

2014年3月25日9:59:52 PM
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl配置警告:HHH000402:使用Hibernate内置连接池(不适用于生产!)2014年3月25日9:59:
52 PM
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
buildCreator INFO:HHH000401:使用URL [jdbc:mysql://
ec2-54-197-214-209)上的驱动程序[com.mysql.jdbc.Driver]。
compute-1.amazonaws.com:3306/ChronoMetriq] 2014年3月25日9:59:52 PM
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
buildCreator INFO:HHH000046:连接属性:{user = root,password = *}
2014年3月25日,下午9:59:52
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl
buildCreator INFO:HHH000006:自动提交模式:false 2014年3月25日,org.hibernate.engine
.jdbc.connections.internal.DriverManagerConnectionProviderImpl配置INFO:HHH000115:hibernate连接池大小:10(min
= 1)创建SessionFactory时出错:
*调用Driver#connect
null时 出错 null
2014年3月25日晚上9:59:52 com.sun。 jersey.spi.container.ContainerResponse
mapMappableContainerException严重:无法将RuntimeException映射到响应,重新抛出到HTTP容器
java.lang.NullPointerException
在ca.chronometriq.webterm.restapi.Process.getQueueName(Process.java:132)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect
.com.sun.jersey.spi.container.JavaMethodInvokerFactory $
1.invoke(JavaMethodInvokerFactory.java:60)上的java.lang.reflect.Method.invoke(Method.java:606)上的.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(com.sun.jersey.server.impl.model.method.dispatch.Abs​​tractResourceMethodDispatchProvider
$
TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)处ResourceJavaMethodDispatcher.java:75)在com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)在com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:
147),位于com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108),位于com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:
147),com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84),com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)在com.sun.jersey.server.impl.application.WebApplicationImpl上的com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)处。com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)上的handleRequest(WebApplicationImpl.java:1349)com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent。
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)上的com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)上的java:416)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain)处org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)处javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
.java:210),位于org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222),位于org.apache.catalina.authenticator.Authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502),位于org.apache.catalina.authentic(StandardContextValve.java:123),位于org.apache.catalina.core.StandardHostValve
org.org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)的org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)的.invoke(StandardHostValve.java:171)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)处的apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor。
java:1024),网址为org.apache.coyote.AbstractProtocol $
AbstractConnectionHandler。java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)上org.apache.tomcat.util.net.NioEndpoint
$
SocketProcessor.run(NioEndpoint.java:1686)上的process(AbstractProtocol.java:589)
.util.concurrent.ThreadPoolExecutor $
Worker.run(ThreadPoolExecutor.java:615)在java.lang.Thread.run(Thread.java:744)

这是我的hibernate.cfg.xml文件内容:

    <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
     <session-factory>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://myaddress.compute-1.amazonaws.com:3306/ChronoMetriq</property>
        <property name="hibernate.connection.release_mode">auto</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">********</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="show_sql">true</property>

        <mapping class="ca.chronometriq.webterm.model.Clinic" />
        ...

    </session-factory>
</hibernate-configuration>

这是我们的会话工厂代码:

public class DataObjectFactory {
    @SuppressWarnings({ "rawtypes" })
    private static final ThreadLocal threadLocal = new ThreadLocal();

    private static ServiceRegistry serviceRegistry;
    private static SessionFactory sessionFactory;

    private static Configuration configuration;


    private static final Logger logger = Logger.getLogger(DataAccessInterface.class.getName());


    static {
        try {
            configuration = new Configuration().addResource("hibernate.cfg.xml");
            configuration.configure();

            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);


        } catch (Exception e) {
            System.err.println("Failed to create sessionFactory object." + e);
        }
    }

    private DataObjectFactory(){
    }

    @SuppressWarnings("unchecked")
    public static Session getSession() throws HibernateException {

        Session session = (Session) threadLocal.get();

        if (session == null || !session.isOpen()) {
            if (sessionFactory == null) {
                rebuildSessionFactory();
            }
            session = (sessionFactory != null) ? sessionFactory.openSession() : null;

            threadLocal.set(session);
        }
        return session;
    }

public static void rebuildSessionFactory() {
    try {
        configuration = new Configuration().addResource("hibernate.cfg.xml");
        configuration.configure();

        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    } catch (HibernateException e) {
        System.err.println("Error Creating SessionFactory: " + e.getMessage());
    }
}

@SuppressWarnings("unchecked")
public static void closeSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    threadLocal.set(null);
    if (session != null) {
        session.flush();
        session.close();
    }
}

public static SessionFactory getSessionFactory() {
    configuration = new Configuration().addResource("hibernate.cfg.xml");
    configuration.configure();

    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    return sessionFactory;
}

public static Configuration getConfiguration() {
    return configuration;
}


    public static void closeRegistry() throws Exception{
        if(serviceRegistry!= null) {
            StandardServiceRegistryBuilder.destroy(serviceRegistry);
        }
    }
}

知道为什么会这样吗?


阅读 184

收藏
2020-06-16

共1个答案

一尘不染

错误调用Driver#connect
”通常是当Tomcat无法访问您的数据库时出现的错误。可能是因为您提供了错误的用户名/密码组合,或者是由于您的数据库(在这种情况下为Amazon)可能阻止了源IP的连接。

我建议您从发出请求的位置找到源IP和端口,并将其添加到mysql中的用户权限中。使用IP为“ root”用户授予所有特权。

您可以通过以下方式进行:

授予所有特权 以GRANT OPTION选项以’the_password’标识为’root’@’xxxx’;

其中xxxx是源IP(如果mysql和tomcat在同一服务器上,并且是Amazon
EC2实例,请确保使用您的EC2实例的私有ip以及私有DNS(例如ip-xxxx)执行该查询。 ec2。内部)。

最后,MySQL数据库中的授予用户应该是主机的“ root”用户:127.0.0.1 / localhost / :: 1 / EC2专用IP /
EC2专用DNS(以及其他外部IP,如果您打算从以下位置调用它)在本地主机之外)。

希望能帮助到你。最高

2020-06-16