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.
Related
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>
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)
Im using JNDI for datasource defined in our application server(websphere) and it is configured to manage the db connection pool. I have a service deployed on that server which also defines db connection pooling as per the cofiguration below.
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="test">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.c3p0.min_size" value="1"/>
<property name="hibernate.c3p0.max_size" value="10"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.ejb.autodetection" value="hbm"/>
<property name="hibernate.use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>
Now, my goal is to completely remove the db connection pooling management on the service and let the application service handle it. If I remove the two c3p0 entries, does that mean no db connection pooling is happening inside the service and all are managed by the application server?
Im new to this kind of thing, and inputs or reference is highly appreciated. Thanks
[UPDATE1]
From C3P0ConnectionProvider "Hibernate will use this by default if the hibernate.c3p0.* properties are set."
Based on the above xml, I already removed the default pooling. Now if I did not define any pooling provider on the service then I essentially removed the pooling on the service right? I feel this is a dumb question now but please confirm if this is correct. Thanks :)
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.
I'm using JBoss 4.2.3 and I deployed two ears called triad-1.0.ear and reportservices-1.0.ear, the thing is that I want to use the entity manager of the project triad in the project reportservices. This is the architecture JBoss follows:
triad-1.0.ear:
triad-core-1.0.jar:
META-INF:
MANIFEST.MF
components.xml
ejb-jar.xml
jboss.xml
persistence.xml
reportservices-1.0.ear:
reportservices-core-1.0.jar:
META-INF:
MANIFEST.MF
components.xml
ejb-jar.xml
jboss.xml
persistence.xml
this is my attempt to make the entitymanager global between ear in the persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Attention: Generated code! Do not modify by hand!
Generated by: persistence.xml.vsl in andromda-ejb3-cartridge.
-->
<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_1_0.xsd"
version="1.0">
<persistence-unit name="triad">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/triad</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
<property name="jboss.entity.manager.jndi.name" value="java:/triadFactory"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/triadFactory"/>
</properties>
</persistence-unit>
</persistence>
I'm sorry but the question doesn't make much sense in my opinion (I don't even get what you mean by "make public the persistence.xml"). Just in case, it is possible:
to use the same datasource in several persistence units.
to (re)use a persistence unit in several applications (provided the packaging make it possible).
My suggestion would be to explain what you would like to do in plain english (forget the technical details for now).
i finally solved mi problem with the injection, you need to set this properties in the persistence.xml and check in jboss jmx console, in the option of jdni view if the injection was correct the properties here's an example of the persistence.xml
<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_1_0.xsd" version="1.0">
<persistence-unit name="example">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/example</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
<property name="jboss.entity.manager.jndi.name" value="java:/example"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/example"/>
</properties>
</persistence-unit>
</persistence>`
the properties that allowing the injection are "jboss.entity.manager.jndi.name" and "jboss.entity.manager.factory.jndi.name"
notes the data have the same name that the data source but called by his jdni name sets in the xml of the datasource project.