Is this xml well formed for a context.xml? - mysql

I'm working out of Murach's Java servlets and JSP, chapter 14 on connecting to MySQL, which I am not able to do. I'm using Netbeans and Tomcat. If I try to run the code with the following context.xml file then the code won't run. It says
[Fatal Error] :3:2: The markup in the document following the root element must
be well-formed.
/Users/user/NetBeansProjects/LearnYou/nbproject/build-impl.xml:724:3
Deployment error: Tomcat configuration file /Users/user/NetBeansProjects/LearnYou/web/META-INF/context.xml seems to be broken. Please make sure it is parseable and valid.
If I comment out all but the first line then the code runs but never connects to MySQL.
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/projectname"/>
<Resource
name="jdbc/ULearniversity" auth="Container"
maxActive="100" maxIdle="50" maxWait="60000"
username="uName" password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/MyDataBase?autoReconnect=true"
logAbandoned="true" removeAbandoned="true"
removeAbandonedTimeout="60" type="javax.sql.DataSource"
/>
</Context>

You're closing the Context tag immediately. Remove the / before the >.
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/projectname">
<Resource
name="jdbc/ULearniversity" auth="Container"
maxActive="100" maxIdle="50" maxWait="60000"
username="root" password="rootpassword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/MyDataBase?autoReconnect=true"
logAbandoned="true" removeAbandoned="true"
removeAbandonedTimeout="60" type="javax.sql.DataSource"
/>
</Context>

Related

web services.Error: com.mysql.jdbc.CommunicationsException: Communications link failure java.io.EOFException [duplicate]

My webapp is running on Tomcat 5.5, I declared the datasource in web.xml:
<resource-ref>
<res-ref-name>jdbc/OrdiniWebDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
In context.xml (tomcat conf):
<Resource
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/OrdiniWebDS"
password="[mypassword]"
type="javax.sql.DataSource"
url="jdbc:mysql://[myHost:port]/ordiniweb"
username="[myusername]"
/>
The database is a MySql 5.0.
Everything works well except that sometimes, after several hours of "unuse", at first access I got this Exception:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.io.EOFException
STACKTRACE:
java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1956)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2368)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3255)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1428)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
at com.blasetti.ordiniweb.dao.OrdiniDAO.caricaOrdine(OrdiniDAO.java:263)
...
** END NESTED EXCEPTION **
Last packet sent to the server was 0 ms ago.
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2579)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)
com.mysql.jdbc.Connection.execSQL(Connection.java:3255)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293)
com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1428)
org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
com.blasetti.ordiniweb.dao.OrdiniDAO.caricaOrdine(OrdiniDAO.java:263)
...
I need to refresh and it works well again. Any idea? Thanks.
MySQL drops unused connections after a while because it assumes that the other side forgot to close it.
What you need to do is to configure a check that Tomcat should apply before it reuses a pooled connection. To do this, add ?autoReconnect=true&useUnicode=true&characterEncoding=utf8 to the end of the URL and add validationQuery="Select 1" to the Resource element:
<Resource
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/OrdiniWebDS"
password="[mypassword]"
type="javax.sql.DataSource"
url="jdbc:mysql://[myHost:port]/ordiniweb?autoReconnect=true&useUnicode=true&characterEncoding=utf8"
username="[myusername]"
validationQuery="Select 1"
/>
[EDIT] This page gives more details: Configuring a MySQL Datasource in Apache Tomcat

Import MySQL data base resource from an xml file in java web

I'm working in a java web project in netbeans.
I actually can connect my project to mysql using the following resource in the context.xml
/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/Test">
<Resource auth="Container"
driverClassName="com.mysql.jdbc.Driver"
logAbandoned="true"
maxActive="100"
maxIdle="30"
maxWait="30"
name="jdbc/primary"
removeAbandoned="true"
removeAbandonedTimeout="120"
type="javax.sql.DataSource"
url="jdbc:mysql://<<host>>/<<schema>>"
username="<<username>>"
password="<<password>>"
/>
</Context>
But I need separate the resource information to another file, how I can do it?
I tryng this:
/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/Test">
<import resource="/WEB-INF/resource.xml"/>
</Context>
/WEB-INF/resource.xml
<?xml version="1.0" encoding="UTF-8"?>
<Resource auth="Container"
driverClassName="com.mysql.jdbc.Driver"
logAbandoned="true"
maxActive="100"
maxIdle="30"
maxWait="30"
name="jdbc/primary"
removeAbandoned="true"
removeAbandonedTimeout="120"
type="javax.sql.DataSource"
url="jdbc:mysql://<<host>>/<<schema>>"
username="<<username>>"
password="<<password>>"
/>

How to sql:setdatasource where datasource is defined in context.xml and web.xml

I am trying to use my defined data source from context.xml and web.xml in my jsp page using sql:setdatasource.
I have been able to directly specify the connection using the following:
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/bar_db" user="foo" password="bar123"/>
<sql:query var="result" dataSource="${snapshot}">
SELECT * FROM foo_tb
</sql:query>
I would like to get the username, pasword and url out of this file using web.xml and context.xml.
Using something like this:
<sql:setDataSource var="conn" dataSource="jdbc/bar_db"
Here is what I tried -
Web.xml
<web-app ...> ...
<resource-ref>
<description>DB Conn</description>
<res-ref-name>jdbc/bar_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref></web-app>
Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/FieldNotifications">
<Resource auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="20"
maxIdle="8" maxWait="10000"
name="jdbc/bar_db"
password="bar123"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/bar_db"
username="foo"/>
</Context>
I am using the following tools: Netbeans 8.1, MySQL 5.7, Connector/J
Update:
Looking for something other than initParams, but it is my current stop-gap.
I have created context-params & am using the following for now:
<context-param>
<param-name>dbaddress</param-name>
<param-value>jdbc:mysql://localhost:3306/bar_db</param-value>
</context-param>
<context-param>
<param-name>dbuser</param-name>
<param-value>foo</param-value>
</context-param>
<context-param>
<param-name>dbpassword</param-name>
<param-value>bar123</param-value>
</context-param>
In JSP, I use
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
url="${initParam['dbaddress']}" user="${initParam['dbuser']}"
password="${initParam['dbpassword']}"/>
Issue remains open:
How to set datasource using context.xml and web.xml?
By defining a resource or connection pool. Somehow the driver is not accessible.

How to configure Tapestry5, Hibernate, Tomcat7, JNDI, Mysql

I've been trying to figure out how to configure Tapestry5, Hibernate, Tomcat7, and Mysql, but have not been able to get it to work. Could someone possible tell me what I'm doing wrong?
To began with, Tapestry is already configured to work with hibernate and jetty without issue, I'm having difficulties setting up a connection pool with Tomcat7
What I have thus far.
MyApp Configuration
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/rolemanager</property>
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">false</property>
</session-factory>
</hibernate-configuration>
myapp/Web Pages/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>rolemanager Tapestry 5 Application</display-name>
<context-param>
<!-- The only significant configuration for Tapestry 5, this informs Tapestry
of where to look for pages, components and mixins. -->
<param-name>tapestry.app-package</param-name>
<param-value>com.mycompany.rolemanager</param-value>
</context-param>
<filter>
<filter-name>app</filter-name>
<filter-class>org.apache.tapestry5.TapestryFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>app</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<resource-ref>
<description>MyDatabase Description</description>
<res-ref-name>jdbc/rolemanager</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
Tomcat Configuration
Tomcat7/conf/server.xml
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="jdbc/rolemanager" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="test" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/rolemanager"/>
</GlobalNamingResources>
Tomcat7/conf/context.xml
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<ResourceLink global="jdbc/rolemanager" name="jdbc/rolemanager" type="javax.sql.DataSource"/>
</Context>
Console Error
I'm getting the following console error.
SEVER: Parse error in application web.xml file at jndi:localhost/WEB-INF/web.xml
java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addResourceRef
It appeared to be an issue with a dependency in my pom adding conflicting classes. I was able to remove the conflicting classes from my class path and fix resolve my issue.

context.xml in Tomcat without DB password

I have a servlet application running on Tomcat 7 and wish to connect to MySQL locally.
My local MySQL user does not have a password, and I wish to keep it that way for convenience.
However, if I do not use the password attribute under Resource in context.xml, it tries to connect to the DB without the username even though I have set it.
How can I connect without a password?
Below is my context.xml file, if it matters.
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="whirlwin"
password="removeMe"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/theDB?autoReconnect=true"/>
</Context>
Have you tried leaving empty string for the password?
If this doesn't help, post what you get running mysql -uwhirlwin -hlocalhost from the command line.