How to configure Tapestry5, Hibernate, Tomcat7, JNDI, Mysql - 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.

Related

OPENSHIFT + j_security_check problems

I have Java code with j_security_check that works on a local Tomcat + MySQL server. I am not able to get it working on the Openshift Jboss (Tomcat) cartridge.
----web.xml code snippet:----
<security-constraint>
<web-resource-collection>
<web-resource-name>Validate</web-resource-name>
<url-pattern>/user/*</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<!-- Authorize the service and programmer roles -->
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
</security-constraint>
<!-- Use form-based authentication to provide access -->
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/validation/login.jsp</form-login-page>
<form-error-page>/validation/login_error.jsp</form-error-page>
</form-login-config>
--------META-INF/context.xml:-----
<Context path="/musicStoreOTUser">
<Resource auth="Container"
driverClassName="com.mysql.jdbc.Driver"
logAbandoned="true"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/musicDB"
password="ask4_sgh"
removeAbandoned="true"
removeAbandonedTimeout="60"
type="javax.sql.DataSource"
url="jdbc:mysql://127.7.245.2:3306/mydb"
username="admin1234"/>
<Realm className="org.apache.catalina.realm.JDBCRealm"
connectionName="admin1234"
connectionPassword="ask4_sgh"
connectionURL="jdbc:mysql://127.7.245.2:3306/mydb"
dataSourceName="jdbc/musicDB"
debug="99"
driverName="com.mysql.jdbc.Driver"
roleNameCol="Rolename"
userCredCol="Password"
userNameCol="EmailAddress"
userRoleTable="RoleTable"
userTable="PassTable"/>
When I log in in the login form, I simply get Login incorrect. No messages are logged in the jbossews.log file.
Appreciate any thoughts.
Thanks,

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.

Trying to install HikariCP on tomcat 7 and getting javax.naming.NamingException on startup

I am trying to configure HikariCP as my datasource connection pool on tomcat 7 with mysql. Here is my context file...
<?xml version="1.0" encoding="UTF-8"?>
<Context allowCasualMultipartParsing="true">
<Resource name="jdbc/application" auth="Container"
factory="com.zaxxer.hikari.HikariJNDIFactory"
type="javax.sql.DataSource"
minimumIdle="5"
maximumPoolSize="10"
connectionTimeout="300000"
dataSource.implicitCachingEnabled="true"
dataSource.user="root"
dataSource.password="pass"
dataSource.url="jdbc:mysql://localhost:3306/Database"/>
</Context>
But on startup I get this error...
WARNING: Failed to retrieve JNDI naming context for container
javax.naming.NamingException: No naming context bound to this class loader
How can I configure HikariCP-2.3.0 on tomcat?
may be this could help you
Configuring the Tomcat Definitions
Location of your JNDI datasource definitions depends upon the scope for the connections. You can define them globally by specifying them in Tomcat's conf/server.xml and conf/context.xml, or you can scope them to individual applications by defining them in conf/Catalina/localhost/WebAppContext.xml (where WebAppContext is the web application context for the app, basically the directory name from Tomcat's webapps directory).
<Resource name="jdbc/LiferayPool" auth="Container"
factory="com.zaxxer.hikari.HikariJNDIFactory"
type="javax.sql.DataSource"
minimumIdle="5"
maximumPoolSize="10"
connectionTimeout="300000"
dataSourceClassName="org.postgresql.ds.PGSimpleDataSource"
dataSource.url="jdbc:postgresql://localhost:5432/lportal"
dataSource.implicitCachingEnabled="true"
dataSource.user="user"
dataSource.password="pwd" />
https://community.liferay.com/blogs/-/blogs/tomcat-hikaricp

Tomcat 7 (JBoss EWS 2.0) + MySQL 5.1 + Hibernate - OpenShift - No persistence unit found

Every time I try to reach my app on OpenShift I receive the "No Persistence provider for EntityManager named appname" error.
I have been struggling to resolve this problem for two consecutive days and I still did not find an answer.
I have tried all the possible configuration I found over the web.
My current configuration:
META-INF\persistence.xml
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="appname" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/comp/env/jdbc/appname</jta-data-source>
<class>myorg.mypackage.MyClass</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
</properties>
</persistence-unit>
</persistence>
.openshift\config\context.xml
<Context>
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/appname" password="mypass"
type="javax.sql.DataSource" url="jdbc:mysql://myurl:3306/appname"
username="myuser" />
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
.openshift\config\server.xml
<Host name="localhost" appBase="webapps" unpackWARs="false" autoDeploy="true">
<Context docBase="appname" path="/appname" reloadable="true" source="org.eclipse.jst.jee.server:appname" />
</Host>
The exception
javax.persistence.PersistenceException: No Persistence provider for EntityManager named appname
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at org.andvicoso.shopand.model.dao.base.GenericDaoJPA.<init>(GenericDaoJPA.java:20)
at org.andvicoso.shopand.model.dao.ProductDaoJPA.<init>(ProductDaoJPA.java:8)
at org.andvicoso.shopand.web.ViewHelper.getProductDao(ViewHelper.java:31)
at org.andvicoso.shopand.web.ViewHelper.getHighlights(ViewHelper.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:106)
at org.apache.jsp.index_jsp._jspx_meth_c_005fset_005f6(index_jsp.java:260)
Can anyone help me???
Thanks in advance.
I faced this issue and found that my META-INF was under src\main\java which was causing this error. When I moved META-INF to src\main\resources... it worked.

Is this xml well formed for a context.xml?

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>