JDBC连接池到MySQL的OpenShift。

时间:2022-05-22 03:52:44

First of all, I'm a computer science student, and I'm not very much into computer science world yet (i.e. I've little experience doing stuff on my own). So sorry for not having all the knowledge possible on it.

首先,我是一名计算机科学专业的学生,我还不太喜欢计算机科学领域(也就是说,我很少有自己的工作经验)。很抱歉,没有所有的知识。

Then, in one of my classes I learnt how to create web application with java (jsp, beans, etc.) plus all the client-side stuff (html, css, javascript, ect.).

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

I work on NetBeans IDE.

我在NetBeans IDE上工作。

To connect to a MySQL database, I use connection pooling in this way:

要连接到一个MySQL数据库,我用这种方式使用连接池:

1) Add MySQL JDBC Driver jar

1)添加MySQL JDBC驱动程序jar。

2) A DBConnect.java java class with a method that returns a connection:

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:

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

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>

Now, I created a small project and I wanted to publish it online for free. I ran into OpenShift and I managed to push all my files on it (even if the folders' schema is different).

现在,我创建了一个小项目,我想在网上免费发布它。我遇到了OpenShift,我成功地将所有文件都推到它上面(即使文件夹的模式不同)。

The problem is that the connection pooling doesn't work, and I don't have a clue on what to do.

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

Running the application, these are the exceptions:

运行应用程序,这些是例外情况:

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 is in /WEB_INF/lib and pom.xml has:

mysql-connector jar位于/WEB_INF/lib和pom中。xml有:

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

Maybe the solution is quite simple but I don't know what to do. Thank you.

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

2 个解决方案

#1


1  

I think the problem is your web.xml file. It's redundant. Context.xml specifies a data source with the appropriate configuration and then web.xml specifies one without a URL or driver class name.

我认为问题在于你的网络。xml文件。这是多余的。上下文。xml指定一个具有适当配置和web的数据源。xml指定了一个没有URL或驱动程序类的名称。

Try removing this resource-ref block from web.xml and try again:

尝试从web删除这个资源引用块。xml并再次尝试:

<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>

You also have extra quotes in your URL attribute in context.xml:

在context.xml中,您的URL属性中还有额外的引号。

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

Make this:

让这个:

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

#2


1  

I'va ran into this problem myself on OpenShift and also on my Tomcat(that it was installed on personal PC).

我自己在OpenShift和我的Tomcat上遇到了这个问题(它安装在个人电脑上)。

It seems that the problem is related to the context.xml file. Even if i edited the context.xml file that was in my cloned openshift project it seemed that the problem didn't dissappear. I've managed to solve this on my personal machine by accessing eclipse server directory: /Servers/Tomcat v7.0 Server at localhost-config/context.xml

问题似乎与上下文有关。xml文件。即使我编辑了上下文。在我克隆的openshift项目中,xml文件似乎并没有出现问题。我通过访问eclipse服务器目录:/ server /Tomcat v7.0服务器,在localhost-config/context.xml中成功地解决了这个问题。

In the context.xml i had to manually add:

在上下文。我必须手动添加:

<Resource name="jdbc/MySQLPool"
          url="jdbc:mysql://localhost:3306/sakila"
          driverClassName="com.mysql.jdbc.Driver"
          username="root"
          password="nbuser"
          auth="Container"
          type="javax.sql.DataSource"
          maxActive="20"
          maxIdle="5"
          maxWait="10000"
        />

Saved the file and everything was solved for the Tomcat on my PC.

保存了文件,在我的PC上解决了Tomcat的所有问题。

At this moment i'm still trying to solve the OpenShift issue. I am using eclipse, and even if i edit the context.xml that is in my OpenShift clone, it seems that somehow, the context.xml file on the OpenShift-Tomcat platform must be accessed and updated like in the previous example.

目前我还在尝试解决OpenShift的问题。我使用eclipse,即使我编辑上下文。在我的OpenShift克隆中,它看起来是这样的。在OpenShift-Tomcat平台上的xml文件必须像前面的示例一样访问和更新。

Hope this help !

希望这次的帮助!

Update: I've managed to solve it for the OpenShift platform in the same manner. By using Filezilla(guide for openshift can be found here: https://blog.openshift.com/using-filezilla-and-sftp-on-windows-with-openshift/ ) i've connected to the server, accesed the Tomcat directory(in my case: jbossews/conf/context.xml) and i've manually edited the file by adding the same xml Resource like above.

更新:我已经用同样的方式解决了OpenShift平台的问题。通过使用Filezilla(可以在这里找到openshift的向导:https://blog.openshift.com/usingfilezils/sftp -on-window -with-openshift/),我已经连接到服务器,accesed的Tomcat目录(在我的例子中是:jbossews/conf/context.xml),我通过添加相同的xml资源来手动编辑该文件。

Hope it helps !

希望它可以帮助!

#1


1  

I think the problem is your web.xml file. It's redundant. Context.xml specifies a data source with the appropriate configuration and then web.xml specifies one without a URL or driver class name.

我认为问题在于你的网络。xml文件。这是多余的。上下文。xml指定一个具有适当配置和web的数据源。xml指定了一个没有URL或驱动程序类的名称。

Try removing this resource-ref block from web.xml and try again:

尝试从web删除这个资源引用块。xml并再次尝试:

<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>

You also have extra quotes in your URL attribute in context.xml:

在context.xml中,您的URL属性中还有额外的引号。

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

Make this:

让这个:

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

#2


1  

I'va ran into this problem myself on OpenShift and also on my Tomcat(that it was installed on personal PC).

我自己在OpenShift和我的Tomcat上遇到了这个问题(它安装在个人电脑上)。

It seems that the problem is related to the context.xml file. Even if i edited the context.xml file that was in my cloned openshift project it seemed that the problem didn't dissappear. I've managed to solve this on my personal machine by accessing eclipse server directory: /Servers/Tomcat v7.0 Server at localhost-config/context.xml

问题似乎与上下文有关。xml文件。即使我编辑了上下文。在我克隆的openshift项目中,xml文件似乎并没有出现问题。我通过访问eclipse服务器目录:/ server /Tomcat v7.0服务器,在localhost-config/context.xml中成功地解决了这个问题。

In the context.xml i had to manually add:

在上下文。我必须手动添加:

<Resource name="jdbc/MySQLPool"
          url="jdbc:mysql://localhost:3306/sakila"
          driverClassName="com.mysql.jdbc.Driver"
          username="root"
          password="nbuser"
          auth="Container"
          type="javax.sql.DataSource"
          maxActive="20"
          maxIdle="5"
          maxWait="10000"
        />

Saved the file and everything was solved for the Tomcat on my PC.

保存了文件,在我的PC上解决了Tomcat的所有问题。

At this moment i'm still trying to solve the OpenShift issue. I am using eclipse, and even if i edit the context.xml that is in my OpenShift clone, it seems that somehow, the context.xml file on the OpenShift-Tomcat platform must be accessed and updated like in the previous example.

目前我还在尝试解决OpenShift的问题。我使用eclipse,即使我编辑上下文。在我的OpenShift克隆中,它看起来是这样的。在OpenShift-Tomcat平台上的xml文件必须像前面的示例一样访问和更新。

Hope this help !

希望这次的帮助!

Update: I've managed to solve it for the OpenShift platform in the same manner. By using Filezilla(guide for openshift can be found here: https://blog.openshift.com/using-filezilla-and-sftp-on-windows-with-openshift/ ) i've connected to the server, accesed the Tomcat directory(in my case: jbossews/conf/context.xml) and i've manually edited the file by adding the same xml Resource like above.

更新:我已经用同样的方式解决了OpenShift平台的问题。通过使用Filezilla(可以在这里找到openshift的向导:https://blog.openshift.com/usingfilezils/sftp -on-window -with-openshift/),我已经连接到服务器,accesed的Tomcat目录(在我的例子中是:jbossews/conf/context.xml),我通过添加相同的xml资源来手动编辑该文件。

Hope it helps !

希望它可以帮助!