java - OpenShift 上到 MySQL 的 JDBC 连接池

标签 java mysql maven jdbc openshift

首先,我是一名计算机科学专业的学生,​​而且我还不太了解计算机科学世界(即我自己做事的经验很少)。很抱歉没有掌握所有可能的知识。

然后,在我的一门类(class)中,我学习了如何使用 java(jsp、bean 等)以及所有客户端内容(html、css、javascript 等)创建 Web 应用程序。

我使用 NetBeans IDE。

为了连接到 MySQL 数据库,我以这种方式使用连接池:

1) 添加 MySQL JDBC 驱动程序 jar

2) DBConnect.java java 类,具有返回连接的方法:

public static Connection getConnection() {
    /* JNDI query to locate the DataSource object */
    Context initContext;
    try {
        initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env"); // JNDI standard naming root
        DataSource ds = (DataSource) envContext.lookup("jdbc/aName");

        /* Ask DataSource for a connection */
        Connection conn;
        try {
            conn = ds.getConnection();
            return conn;
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException("cannot open Connection", ex);
        }
    } catch (NamingException ex) {
        Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException("cannot find DataSource reference", ex);
    }
}

3) web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<resource-ref>
    <description>Resource reference to a DataSource for managing a connection pool.</description>
    <res-ref-name>jdbc/aName</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

</web-app>

4) context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/myApp">
    <Resource 
     auth="Container" 
     driverClassName="com.mysql.jdbc.Driver" 
     maxActive="100" 
     maxIdle="30" 
     maxWait="10000" 
     name="jdbc/aName" 
     username="username" 
     password="password" 
     type="javax.sql.DataSource" 
     url="jdbc:mysql://"whateverhost":"whateverport"/dbSchema?autoReconnect=true"/>
</Context>

现在,我创建了一个小项目,我想免费在线发布它。我遇到了 OpenShift,并设法将所有文件推送到它上面(即使文件夹的架构不同)。

问题是连接池不起作用,我不知道该怎么做。

运行应用程序,以下是异常(exception)情况:

java.lang.RuntimeException: cannot open Connection
....

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
....

java.sql.SQLException: No suitable driver
....

mysql-connector jar 位于/WEB_INF/lib 中,pom.xml 具有:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.25</version>
</dependency>

也许解决方案很简单,但我不知道该怎么做。 谢谢。

最佳答案

我认为问题出在你的 web.xml 文件上。这是多余的。 Context.xml 指定具有适当配置的数据源,然后 web.xml 指定不带 URL 或驱动程序类名称的数据源。

尝试从 web.xml 中删除此 resource-ref block ,然后重试:

<resource-ref>
    <description>Resource reference to a DataSource for managing a connection pool.</description>
    <res-ref-name>jdbc/aName</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

context.xml 中的 URL 属性中还有额外的引号:

url="jdbc:mysql://"whateverhost":"whateverport"/dbSchema?autoReconnect=true"/>

做这个:

url="jdbc:mysql://whateverhost:whateverport/dbSchema?autoReconnect=true"/>

关于java - OpenShift 上到 MySQL 的 JDBC 连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22894499/

相关文章:

java - 当前两个类已经处于基子类关系时,如何在第三个类中继承两个类?

java - 查询没有返回唯一结果: 3; nested exception is javax. persistence.NonUniqueResultException:(Spring JPA项目)

java.lang.NoSuchMethodError : org. springframework.util.ReflectionUtils.accessibleConstructor

java - 如何自己命名一个 servingURL?

Java - MongoDB 不区分大小写,不检查完全匹配

java - web.xml错误500和struts全局异常有什么区别?

mysql - 在多个连接语句中计数

mysql - ORDER BY 无法按预期使用 GROUP BY

java - cxf-rt-frontend-jaxws 的 Maven 依赖项破坏了现有的 SOAP 客户端

maven - 无法获得完整的 Maven 原型(prototype)列表