amazon aws ubuntu java mysql webservice“错误调用Driver#connect”

时间:2014-03-25 22:25:05

标签: java hibernate tomcat amazon-web-services tomcat7

我们在tomcat 7 / java7 / hibernate / mysql5上运行了一些Web服务。 一切都在我的电脑上的localhost tomcat服务器上运行得很好但是当我们在AWS Ubuntu服务器上部署应用程序时,我们收到以下错误消息:

  
    

2014年3月25日下午9:59:52 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     配置WARN:HHH000402:使用Hibernate内置连接池     (不用于生产用途!)Mar 25,2014 9:59:52 PM     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     buildCreator INFO:HHH000401:使用驱动程序[com.mysql.jdbc.Driver] at     网址     [JDBC:MySQL的://ec2-54-197-214-209.compute-1.amazonaws.com:3306 / ChronoMetriq]     2014年3月25日下午9:59:52     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     buildCreator INFO:HHH000046:连接属性:{user = root,     密码= **} 2014年3月25日下午9:59:52     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     buildCreator信息:HHH000006:自动提交模式:假2014年3月25日     晚上9:59:52     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     配置INFO:HHH000115:Hibernate连接池大小:10(min = 1)     创建SessionFactory时出错:**调用Driver#connect错误 null Mar     25,2014 9:59:52 PM com.sun.jersey.spi.container.ContainerResponse     mapMappableContainerException SEVERE:RuntimeException不能     被映射到响应,重新投掷到HTTP容器     的显示java.lang.NullPointerException             at ca.chronometriq.webterm.restapi.Process.getQueueName(Process.java:132)             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)             at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)             at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)             at java.lang.reflect.Method.invoke(Method.java:606)             at com.sun.jersey.spi.container.JavaMethodInvokerFactory $ 1.invoke(JavaMethodInvokerFactory.java:60)             at com.sun.jersey.server.impl.model.method.dispatch.Abs​​tractResourceMethodDispatchProvider $ TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)             at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)             at 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)             at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)             at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)             at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)             at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)             at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)             在com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)             在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)             在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)             在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)             在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)             在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)             在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)             在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)             在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)             at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)             在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)             在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)             在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)             在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)             在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1024)             at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589)             在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1686)             在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)             at java.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);
        }
    }
}

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:5)

'调用Driver#connect 时出错通常是Tomcat无法访问您的数据库时遇到的错误。可能是因为您提供了错误的用户名/密码组合,或者因为您的数据库(或那种情况下的亚马逊)可能阻止了与源IP的连接。

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

您可以致电:

上授予所有特权。 TO'root'@'x.x.x.x'由'the_password'识别,带有授权选项;

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

最后,MySQL数据库中的授权用户应该是来自主机的“root”:127.0.0.1 / localhost / :: 1 /您的EC2私有IP /您的EC2私有DNS(以及您计划的其他外部IP)从localhost外面调用它。)

希望它有所帮助。 最大