Pregunta

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

I work on NetBeans IDE.

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

1) Add MySQL JDBC Driver jar

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

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>

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

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:

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

¿Fue útil?

Solución

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.

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

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

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

Make this:

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

Otros consejos

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

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

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.

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.

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.

Hope it helps !

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top