java - JDBC connection pool to MySQL on OpenShift -
first of all, i'm computer science student, , i'm not computer science world yet (i.e. i've little experience doing stuff on own). sorry not having knowledge possible on it.
then, in 1 of classes learnt how create web application java (jsp, beans, etc.) plus client-side stuff (html, css, javascript, ect.).
i work on netbeans ide.
to connect mysql database, use connection pooling in way:
1) add mysql jdbc driver jar
2) dbconnect.java java class method returns connection:
public static connection getconnection() { /* jndi query locate 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 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 datasource managing 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, created small project , wanted publish online free. ran openshift , managed push files on (even if folders' schema different).
the problem connection pooling doesn't work, , don't have clue on do.
running application, these exceptions:
java.lang.runtimeexception: cannot open connection .... org.apache.tomcat.dbcp.dbcp.sqlnestedexception: cannot create jdbc driver of class '' connect url 'null' .... java.sql.sqlexception: no suitable driver ....
mysql-connector jar in /web_inf/lib , pom.xml has:
<dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.25</version> </dependency>
maybe solution quite simple don't know do. thank you.
i think problem web.xml file. it's redundant. context.xml specifies data source appropriate configuration , web.xml specifies 1 without url or driver class name.
try removing resource-ref
block web.xml , try again:
<resource-ref> <description>resource reference datasource managing 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 have quotes in url attribute in context.xml:
url="jdbc:mysql://"whateverhost":"whateverport"/dbschema?autoreconnect=true"/>
make this:
url="jdbc:mysql://whateverhost:whateverport/dbschema?autoreconnect=true"/>
Comments
Post a Comment