I am migrating a web application from Tomcat to Wildfly.
The application was using a configuration file stored under tomcat.
I want to avoid having configuration files and point the application to read these files.
I am looking to have the configuration read from Wildfly system like standalone.xml for instance.
what is the best approach?
Thanks
Please use persistence.xml file in Java app for definition make connection to database from standalone.xml file from WildFly
Excample persistence.xml - connection is get from WildFly. Please add JNDI name java:/PostgresDS in WildFly
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="em1">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/PostgresDS</jta-data-source>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Related
I have developed a JAVA Rest Service (JDK 1.8) which uses Kundera (V3.2) for a Cassandra Database connection. If I run my application inside eclipse with a Tomcat-Server everything works well. If I want to deploy .war file on another Tomcat 7 the server does not start because of following error:
com.impetus.kundera.loader.MetamodelLoaderException: Error while retreiving and storing entity metadata, Caused by : .
After searching for some solution I found out that the problem can be that I have my entities in an separated jar (which is a dependency in the war file) and and not in the application itself. If this is the problem I have to add the <jar-file> tag to my persistence.xml (https://github.com/impetus-opensource/Kundera/issues/90).
My persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="cassandra_pu" transaction-type="RESOURCE_LOCAL">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>com.asdf.booking.beans.kundera.Account</class>
<class>com.asdf.booking.beans.kundera.AccountCard</class>
<class>com.asdf.booking.beans.kundera.AccountType</class>
<class>com.asdf.booking.beans.kundera.Booking</class>
<class>com.asdf.booking.beans.kundera.Circle</class>
<class>com.asdf.booking.beans.kundera.AccountSequence</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="kundera.nodes" value="1.1.1.1" />
<property name="kundera.port" value="9160" />
<property name="kundera.keyspace" value="booking" />
<property name="kundera.dialect" value="cassandra"/>
<property name="sessionless" value="false" />
<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.thrift.ThriftClientFactory" />
<property name="kundera.annotations.scan.package" value="com.asdf.booking.kundera.beans" />
<property name="kundera.ddl.auto.prepare" value="update" />
</properties>
</persistence-unit>
</persistence>
Is the separate jar-file possibly the problem or should I search for other solutions?
The problem was the separate jar-file for entities. When adding the jar-file tag, pointing to the jar for entities, to the persistence.xml everything is ok.
I have a newbie question, so I hope you can help.
Currently as ORM tool I am using OpenJPA 2.2.1 .
MY CUrrent persistence UNit:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="FleaCircus" transaction-type="JTA">
<description>Flea Circus</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>FleaCircusOracleDS</jta-data-source>
<class>de.carmedialab.db.entities.ApplicationItem</class>
<class>de.carmedialab.db.entities.FleaResult</class>
<class>de.carmedialab.db.entities.FleaResultType</class>
<class>de.carmedialab.db.entities.ItemAttribute</class>
<class>de.carmedialab.db.entities.ItemGroup</class>
<class>de.carmedialab.db.entities.ItemType</class>
<class>de.carmedialab.db.entities.ItemTypeAttribute</class>
<class>de.carmedialab.db.entities.ItemTypeOperationAttribute</class>
<class>de.carmedialab.db.entities.Operation</class>
<class>de.carmedialab.db.entities.OperationAttribute</class>
<class>de.carmedialab.db.entities.OperationType</class>
<class>de.carmedialab.db.entities.Role</class>
<class>de.carmedialab.db.entities.UserAccount</class>
<class>de.carmedialab.db.entities.Measurement</class>
<class>de.carmedialab.db.entities.MeasurementType</class>
<class>de.carmedialab.db.entities.MeasurementAttribute</class>
<class>de.carmedialab.db.entities.MeasurementAttributeType</class>
<class>de.carmedialab.db.entities.Fleet</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="validate" />
<property
name="openjpa.Compatibility.CheckDatabaseForCascadePersistToDetachedEntity"
value="true" />
<!--<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />-->
</properties>
</persistence-unit>
<persistence-unit name="FleaCircusLocal" transaction-type="RESOURCE_LOCAL">
<description>Flea Circus</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<non-jta-data-source>FleaCircusOracleDS</non-jta-data-source>
<class>de.carmedialab.db.entities.ApplicationItem</class>
<class>de.carmedialab.db.entities.FleaResult</class>
<class>de.carmedialab.db.entities.FleaResultType</class>
<class>de.carmedialab.db.entities.ItemAttribute</class>
<class>de.carmedialab.db.entities.ItemGroup</class>
<class>de.carmedialab.db.entities.ItemType</class>
<class>de.carmedialab.db.entities.ItemTypeAttribute</class>
<class>de.carmedialab.db.entities.ItemTypeOperationAttribute</class>
<class>de.carmedialab.db.entities.Operation</class>
<class>de.carmedialab.db.entities.OperationAttribute</class>
<class>de.carmedialab.db.entities.OperationType</class>
<class>de.carmedialab.db.entities.Role</class>
<class>de.carmedialab.db.entities.UserAccount</class>
<class>de.carmedialab.db.entities.Measurement</class>
<class>de.carmedialab.db.entities.MeasurementType</class>
<class>de.carmedialab.db.entities.MeasurementAttribute</class>
<class>de.carmedialab.db.entities.MeasurementAttributeType</class>
<class>de.carmedialab.db.entities.Fleet</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="validate" />
<property
name="openjpa.Compatibility.CheckDatabaseForCascadePersistToDetachedEntity"
value="true" />
<!--<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />-->
</properties>
</persistence-unit>
</persistence>
For the sake of simplicity, persistence-unit "FleaCircus" is what I use ( Container Managed Persistence ). FleaCircusLocal is another test pu I created , where I've created a sample stateless bean that basically starts a new transaction, persist an element and commit it.
However it doesn't sinchronize immediatly and customer complains about it.
Is there any way to make OpenJPA makes whats in context immediatly visible to the DB or is it impossible due to the nature as OpenJPA/JPA was thought ?
For me it is not a big deal since data is visible inside the container ( Apache Geronimo ) however for the customer it is important since he wants to connet using 3rd party tool ( eg. SQL Developer to and see if data was inserted or not .)
I'm creating an OpenJPA application with maven.
The application starts, but if I try to access a database-entity, I get the following exception:
HTTP Status 500 - <openjpa-2.2.2-r422266:1468616 fatal user error> org.apache.openjpa.persistence.ArgumentException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database onnectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl#442ce698".
The persistence.xml is located in META-INF and is under control by Netbeans 8.
The ConnectionDriverName is set as the following:
<property name="openjpa.ConnectionDriverName" value="org.apache.commons.dbcp.BasicDataSource"/>
My src\main\java\META-INF\persistence.xml (class-tags reduced and database-settings changed):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="PersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.example.test.my.entities.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
<property name="openjpa.Log" value="DefaultLevel=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.MySQLDictionary"/>
<property name="openjpa.ConnectionDriverName" value="org.apache.commons.dbcp.BasicDataSource"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=80, PrintParameters=true"/>
<property name="openjpa.ConnectionProperties" value="DriverClassName=com.mysql.jdbc.Driver,
Url=jdbc:mysql://localhost:3306/database?autoReconnect=true,
Username=usr,
Password=pwd,
MaxWait=60000,
TestOnBorrow=true,
validationQuery=select 1"/>
</properties>
</persistence-unit>
</persistence>
I'm using Tomcat 8 (same result for Tomcat 7 or Tomee).
solved it by switching to eclipse, because it seems like netbeans having some problems with the persistence.xml.
In eclipse I had to use the following m2e-connector:
https://github.com/hwellmann/m2eclipse-extras/raw/master/p2/ (eclipse software repository url)
We are using JPA 2.0 and we created the datasource in websphere and tried to access the database through the J2SE application. We are getting Invalid Username and password error. If we give the user name and password in persistence.xml it works fine.
Please anybody explain me why do we have to give the DB credentials in persistence.xml since we have the data source.
Note: Data Source was created successfully and the test was success.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="Printer">
<jta-data-source>jdbc/TestDataSource</jta-data-source>
<properties>
<property name="openjpa.Optimistic" value="false" />
<property name="openjpa.LockManager" value="pessimistic" />
<property name="javax.persistence.jdbc.user" value="admin" />
<property name="javax.persistence.jdbc.password" value="admin#2" />
</properties>
</persistence-unit>
</persistence>
It seems that the datasource is not configured correctly in websphere. Test the database connection through the websphere console to verify the configuration.
A DataSource has 2 methods to get a Connection. It seems that your jpa implementation uses DataSource.getConnection(String username, String password) if you provide the credentials via properties.
The connection properties are intented to use in a Java SE environment. In JEE you should prefer the JNDI lookup. See section 8.2.1.9 of the JPA 2.0 specification.
You don't need to specify the credentials in your application. Just access your database via JNDI, specifying the name of the datasource you've created in the WS.
One way is to configure persistence unit using the pre-configured datasource (Please double check if it is configured correctly - the test feature is available in WS)
<persistence-unit name="default" transaction-type="JTA">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<jta-data-source>
jdbc/MyDataSource
</jta-data-source>
<properties>
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
If the datasource is not configured on the application container side, you may set it up yourself on the application side. For example, you need a number of the applications each one has its own database connection config.
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.logging.level" value="INFO"/>
<property name="toplink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="toplink.jdbc.url" value="jdbc:oracle:thin:#myhost:l521:MYSID"/>
<property name="toplink.jdbc.password" value="tiger"/>
<property name="toplink.jdbc.user" value="scott"/>
</properties>
</persistence-unit>
Your persistence.xml has the config for both ways. Remove the unnecessary code and try again.
I've just migrated from Tomcat to JBoss AS 7.
So, I configured Mysql datasource in JBoss (adding module.xml with associated Jar, adding driver bloc into standalone.xml and configuring datasource through JBoss interface.
No errors when deploying but impossible to get an entityManager (JPA with Hibernate in background).
Indeed, when this code is executed:
Persistence.createEntityManagerFactory("RoomManagement");
I obtain this error :
javax.persistence.PersistenceException: No Persistence provider for
EntityManager named RoomManagement
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
Very strange because I well verified that my persistence.xml does take place into War at WEB-INF/classes/META-INF directory.
My persistence.xml looks like as follow :
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="RoomManagement" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
<class>com.parisdescartes.roommanagement.domain.entities.Address</class>
<class>com.parisdescartes.roommanagement.domain.entities.Building</class>
<class>com.parisdescartes.roommanagement.domain.entities.Civility</class>
<class>com.parisdescartes.roommanagement.domain.entities.EventType</class>
<class>com.parisdescartes.roommanagement.domain.entities.Job</class>
<class>com.parisdescartes.roommanagement.domain.entities.Reservation</class>
<class>com.parisdescartes.roommanagement.domain.entities.Room</class>
<class>com.parisdescartes.roommanagement.domain.entities.RoomType</class>
<class>com.parisdescartes.roommanagement.domain.entities.Tool</class>
<class>com.parisdescartes.roommanagement.domain.entities.User</class>
<class>com.parisdescartes.roommanagement.domain.entities.UserDetail</class>
<class>com.parisdescartes.roommanagement.domain.entities.Schedule</class>
<properties>
<property name="hibernate.connection.autocommit" value="true" />
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
Did I make a mistake or forgot to specify something ?
Remove the hibernate jar from WEB-INF/lib. JBoss has that bundled, so if you have it on the classpath it probably confuses the classloader.