java - Tomcat 和 MYSQL 移植到 AWS

标签 java mysql amazon-web-services tomcat

我有一个运行在 tomcat 7 和 MYSQL 上的 java 7 应用程序。我试图让它在亚马逊 AWS 提供的基本(免费层)平台上运行。我已成功将数据加载到 MYSQL 社区的 RDS 实例,并设置了一个基本 JSP 正常运行的 Elastic Beanstalk 实例。

我是 AWS 的新手,所以主要问题应该是我缺乏知识。

我无法从我的代码连接到数据库。

这是我用来创建连接池的代码:

package com.authz.pap;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBConnections {
    private static DataSource dataSource;

    private static final DBConnections instance = new DBConnections();

    static
    {
        try
        {
            Context c = new InitialContext();
            dataSource = (DataSource)c.lookup("java:comp/env/jdbc/authzDB");
        }
        catch (Exception e)
        {
            dataSource = null;
        }
    }
    private DBConnections()
    {
    }

    public static DBConnections getInstance()
    {
        return instance;
    }

    public static DataSource getGeoServDS() {
        //if(dataSource==null) instance = new DBConnections();
        return dataSource;
    }

}

还有这个:

public DbCon() {

    conn=null;
    ProcessStatus stat = new ProcessStatus();
    stat.function="DbCon.DbCon";
    stat.message = "Initializing DB";
    stat.retcode=0;
    stat.record();
    try {
        conn=DBConnections.getGeoServDS().getConnection();
        conn.setAutoCommit(false);
    } catch (Exception e) {
        stat.message = e.toString();
        stat.retcode=-1;
        stat.record();
    }
}

这是 context.xml 文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/rest">
  <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="8" maxIdle="4" name="jdbc/authzDB" password="xxxx" type="javax.sql.DataSource" url="jdbc:mysql://x.x.x.x:3306/authzDB?autoReconnect=true&amp;allowMultiQueries=true" username="root"/>
  <ResourceLink global="jdbc/authzDB" name="jdbc/authzDB" type="javax.sql.DataSource"/>
</Context>

我得到的是关于数据库初始化的 java.lang.NullPointerException

我做错了什么?

更新:

这是 web.xml 文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" id="WebApp_ID" version="2.5">
  <display-name>paprest</display-name>
  <servlet>
    <servlet-name>PAP rest interface</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.authz.pap.intfc</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>rest interface</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

最佳答案

双重回应:

1- 我无法使用 context.xml 文件中的数据。我有一个不同的解决方案:

private static Connection getRemoteConnection() {
    ProcessStatus stat = new ProcessStatus();
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String dbName = "xxxxDB";
        String userName = "xxxxx";
        String password = "xxxxxx";
        String hostname = "xxxxxxxx";
        String port = "3306";
        String jdbcUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + password + "&autoReconnect=true&amp;allowMultiQueries=true";
        Connection con = DriverManager.getConnection(jdbcUrl);
        return con;
    }
    catch (ClassNotFoundException e) { stat.error(e.toString());}
    catch (SQLException e) { stat.error(e.toString());}
    return null;
  } 

2- 我有一个不同的问题,因为 RDS 实例不是独立创建的,而是与 Elastic Beanstalk 相关的。这允许tomcat和Mysql之间的通信。

关于java - Tomcat 和 MYSQL 移植到 AWS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45436461/

相关文章:

java - 如何获取对象的所有字段和属性,这些字段和属性使用特定注释进行注释?

php - 邮件 PEAR 包不发送电子邮件

php - SQLSTATE[HY000] : General error: 1005 Can't create table `Projectname` .`users` (errno: 150 "Foreign key constraint is incorrectly formed")

apache - 如何在我的 ec2 ami apache 服务器上启用 https

java - 如何使用 hibernate 按列分组并检索所有列

java - 动态实例化类?

java - JanusGraph 地理索引

mysql - 从其他表中查找具有所有值的所有 ID

amazon-web-services - 来自 cli 的 aws ec2 实例 IP 地址 aws ec2 描述实例

amazon-web-services - AWS WAF - 在 S3 中自动保存 Web 应用程序防火墙日志