Configure hibernate.cfg.xml in OpenShift RedHat servers - java

I'm trying to configure a xWiki server on a OpenShift hosting (Tomcat 6 (JBoss EWS 1.0)). I've never configured a Java server before and I have a issue:
I followed THIS tuto on my local Ubuntu and worked. But reproducing the steps in the OpenShift server I can't find the hibernate.cfg.xml. Looking for files in the directory tree with hibernate I've found the folder jbossews-1.0/jbossews-1.0/work/Catalina/localhost/xwiki/WEB-INF/lib/ with the files:
hibernate-c3p0-3.6.9.Final.jar
hibernate-core-3.6.9.Final.jar
hibernate-validator-4.3.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-commons-annotations-3.2.0.Final.jar
But nothing similar to hibernate.cfg.xml. How can I fix it?

hibernate.cfg.xml is your configuration file for Hibernate, where you specify the dialect, connection driver, url, username, password, etc. of the database.
Example:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://host/database</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
</session-factory>
</hibernate-configuration>
Place this file in the Java resources classpath.
For detail information look hibernate.cfg.xml for Oracle or example by mkyong

This is (should be, anyway) an xWiki installation issue. If you are using the WAR file, then when you put the WAR into the tomcat webapps directory, Tomcat normally will expand the WAR into a directory with the same name. In that directory, in the WEB-INF directory, should be the hibernate.cfg.xml file. Typically, here:
/var/lib/tomcat5.5/webapps/xwiki/WEB-INF/hibernate.cfg.xml
If you have configured your Tomcat to run the WAR without expanding it, then the file will be in the WAR at:
WEB-INF/hibernate.cfg.xml
But in this instance you will have to edit the XML file and put it back into the WAR to configure the application.

I had the same problem, and by stracing tomcat I see it looks at the following places, the long one being in the git repository in my openstack gear:
/var/lib/openshift/518f381fe0b8cd1de2000181/git/tomcat.git/hibernate.cfg.xml
/usr/share/java/tomcat7/hibernate.cfg.xml
I cannot see any correlation between these locations and the classpath either given in the command line or in catalina.properties. The git repository is the cwd of tomcat. Putting hibernate.cfg.xml there does work for now. But I believe as soon as openstack guys realize how messy is to put the cwd there, they will move it away.

Related

hibernate integration with postgres error com.sun.xml.bind.v2.runtime.reflect.opt.Injector <clinit> SEVERE: null

I'm making a simple program to connect hibernate with the Postgres server but I'm getting errors in it. I've also downloaded and used the jar files that are used in another StackOverflow question but the issue isn't getting resolved.
App.java
package com.ayush;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class App
{
public static void main(String[] args) {
System.out.println("Project started..");
SessionFactory factory= new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
System.out.println(factory);
}
}
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="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/myhiber</property>
<property name="connection.username">postgres</property>
<property name="connection.password">password</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Ive also included these jar files in my project .
javax.activation-1.2.0.jar
jaxb-api-2.3.0.jar
jaxb-core-2.3.0.jar
jaxb-impl-2.3.0.jar
And the tree structure is like this for the project
tree structure in my eclipse compiler with the error message
The hibernate config always needs to be located inside the resources folder.
Create a folder named "resources"
Right-click your project < Properties < Java Build Path < Source < Add folder
Add the resource folder as Source
Now move the hibernate config file into the resource folder and you should be good to go.

Eclipse - Hibernate : No suitable driver found for jdbc:mysql://localhost:3306/hibernatedb

I am trying to learn Hibernate by writing a simple java program. I'm using MySQL as the database, and I get the above error when running the program. I saw a number of solutions for this on the internet and tried everything possible, to no avail. What am I doing wrong?
Configuration file:
<?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.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/test/hibernate/student.hbm.xml" />
</session-factory>
</hibernate-configuration>
Console output:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at com.test.hibernate.SimpleTest.main(SimpleTest.java:23)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/hibernatedb
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
... 5 more
Classpath & lib folder:
Most probably you didn't add the mysql-connector library correctly, because in the snapshot from the Run configuration you posted, that library appears within a project called "hibernate test" which contains a file named "mysql-connector-etc.jar", but that is not the way to set a JAR within a classpath.
Do it this way: It's better that you add the "mysql-connector-etc.jar" right within your project's Java Build Path: Pop up the project's contextual menu, and the Build Path > Configure Build Path > Libraries > Add external jars. Then select the mysql-connector JAR and enter. From then on, Eclipse will include this JAR into any execution of your project you should do (so you won't have to care about it anymore).

JBoss system property replacement does not work

My JBoss version is Jboss-as-7.1.1.Final.
I have some system properties in standalone.xml:
<system-properties>
<property name="ims.eas.service.registerSystem" value="registerSystem"/>
<property name="ims.developerMode" value="false"/>
...
</system-properties>
In web.xml refer to them as ${ims.developerMode}. But it does not work.
Watched System.getProperties() in debug mode, system properties are there. It reads them from standalone.xml but not replaces in web.xml. With jboss-eap-6.1 it works pretty well.

Configuring data source - tomee.xml, persistence.xml

I'm doing some EJB with JPA project that maps/persists some entities to mysql database.
I have defined persistence unit in persistence.xml like this:
<persistence-unit name="MyAppPU" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>MyAppDS</jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.jdbc.DBDictionary" value="mysql" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
Then, in tomee/conf/tomee.xml file i have defined data source like this:
<Resource id="MyAppDS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://127.0.0.1:3306/MyAppDB
UserName root
Password 123
JtaManaged true
DefaultAutoCommit false
</Resource>
All this works fine, i create MyApp.jar, deploy it to TomEE server, test it and i get mysql tables in database.
My question is "Is there any other place where I could define data source resource?"
Or it has to be in tomee/conf/tomee.xml file?
Can it be defined somewhere inside application structure, in some xml file, and deployed inside apps jar file to server?
That's the whole point of a JNDI data source, to externalize it outside of your application, so you can modify it without recompiling or repackaging. So it is better to leave it this way.
For testing purpose, some EE server such as JBoss (Wildfly) let you define this in your project.
It might be a bit late to answer this, You can in tomee place the resource definition in WEB-INF/resources.xml.
You only have to set the tomee.xml. If you are using Eclipse, you must copy the tomee.xml into the servers configuration/Tomee to get recognize it into the web project, otherwise you will get troubles.

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/sushi

I have a web application using a jar file(lib) to access a Database.
The jar file when used as a standalone application executes correctly but the webapp received the error:
[01-08-12 13:17:05] - 35266 WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 08001
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myapp
I have read the answers to similar questions but none of them solve my problem.
I am using Maven, where I have added the dependency for the mysql version I am using, 5.1.21. In fact, I have added it for both the Lib and the Webapp.
Before that, I tried to define in Eclipse a Connectivity Driver Definition bound to the same file, that I had copied to WEB-INF/lib in the eclipse project for my Webapp and with the same parameters that I include below for the persistence.xml file
I am not using any java to configure since I do all the configuration in the persistence.xml file(that I have copied to both META-INF folders, the one for the app(lib) and the one for the webapp. I am using Hibernate (4.1.2) through JPA.
The persistence.xml file is like that:
<persistence 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"
version="2.0">
<persistence-unit name="myappPersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider> org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myapp" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
</persistence>
the exception that I get is at Runtime, when launching an Http Request to the app, not at the initialization of the webapp, where I haven't seen any errors but this warning appears:
[01-08-12 13:16:39] - 8782 WARN
org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342:
Could not obtain connection to query metadata : No suitable driver
found for jdbc:mysql://localhost:3306/myapp
I guess this is due to the information being looked up from the database is not necessary at init, and no exception is thrown but the same root cause in both cases.
EDIT:I am using Tomcat 6.0
Any help will be highly appreciated.
It seems that you are missing MySql driver in your project. To solve this you need to add mysql-connector.jar. externally to your project.
Or if you are using a maven project then use the following dependency in your pom.xml file.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency>
You can use a suitable version if needed!!
If you're running your application as a Java Application, Add the JAR file in the Java Build Path, in Eclipse.
Alternatively, I wouldn't put the MSQL jar inside WEB-INF/lib folder of your web project, instead, I would put it in your Application folder library folder, where the Application Server will load your driver and you can access it from the Application Server container.
you should put mysql connector jar with your own code & add jar file with your project.
I also had the same problem some time before, but I solved that issue. There may be different reasons for this exception.
One of them may be that the jar you are adding to your lib folder may be old. Try to find out the latest mysql-connector-jar version and add that to your classpath. It may solve your issue.
I had the same problem with Tomcat 9. Solved by not having the driver jar in both common/lib and WEB-INF/lib (used the former).

Categories

Resources