Tomee with Arquillian using Postgres DB user lacks privilege or object not found - postgresql

I have a problem when running arquillian tests against postgres db using tomee.
With all the info on the web I'm still struggling to get the problem solved.
javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: CREDENTIALS
Error Code: -5501
Call: SELECT ID, PASSWORD, USERNAME FROM credentials WHERE (USERNAME = ?)
bind => [phil]
The DB:
Name: registry
Table: credentials
Sits under manually created Schema: postgres
persistence.xml under directory src/main/resources
<?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="registry" transaction-type="JTA">
<jta-data-source>RegistryDS</jta-data-source>
<non-jta-data-source>UnmanagedRegistryDS</non-jta-data-source>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>za.co.registry.client.login.Credentials</class>
<properties>
<property name="eclipselink.debug" value="OFF"/>
<property name="eclipselink.weaving" value="static"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.logger" value="DefaultLogger"/>
</properties>
</persistence-unit>
</persistence>
tomee.xml
<Resource id="RegistryDS" type="DataSource">
jdbcDriver=org.postgresql.Driver
jdbcUrl=jdbc:postgresql://127.0.0.1:5432/registry
userName=postgres
password=postgres
JtaManaged=true
</Resource>
<Resource id="UnmanagedRegistryDS" type="DataSource">
jdbcDriver=org.postgresql.Driver
jdbcUrl=jdbc:postgresql://127.0.0.1:5432/registry
userName=postgres
password=postgres
JtaManaged=false
</Resource>
pom.xml extract for arquillian tests
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>1.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.0.3.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
arquillian.xml extract
<container qualifier="tomee" default="true">
<configuration>
<property name="httpPort">-1</property>
<property name="stopPort">-1</property>
<property name="dir">target/apache-tomee-remote</property>
<property name="appWorkingDir">target/arquillian-test-working-dir</property>
<property name="properties" />
</configuration>
</container>
The ServiceTest.java file when loading the resources.
#Deployment
public static WebArchive createDeployment() {
WebArchive webArchive = newArchive();
webArchive.addClasses(Credentials.class);
webArchive.addAsResource("META-INF/persistence.xml");
webArchive.addAsResource("META-INF/beans.xml");
return webArchive;
}
And last the test findCredentialsByUsernameTest method in ServiceTest.java
#Test
public void findCredentialsByUsernameTest() {
Credentials login = LoginService.findByUsername("phil");
Assert.assertNotNull(login);
}
I do not start or end any EntityTransaction's in the test class.
It works injecting a EJB when the DB call in the service is removed.
What am I missing in the config or doing wrong for this not to be working?

Ok I think I know what I did wrong.
I need to run the tests against a embedded db.
The steps that I followed to get Arquillian tests to work against a embedded db;
Removed un-managed data source from tomee.xml, I added it because I wanted to use it for my tests. The other data source is still there because it is used when deploying to tomee to connect to postgres db.
<Resource id="UnmanagedRegistryDS" type="DataSource">
jdbcDriver=org.postgresql.Driver
jdbcUrl=jdbc:postgresql://127.0.0.1:5432/registry
userName=postgres
password=postgres
JtaManaged=false
</Resource>
I'm going to connect to HSQLDB.
HSQLDB (HyperSQL DataBase) is the leading SQL relational database software written in Java. It offers a small, fast multithreaded and transactional database engine with in-memory and disk-based tables and supports embedded and server modes
Next I create a second persistence.xml in src/test/resources called test-persistence.xml. Keep in mind the persistence-unit name testDatabase it is going to be used in the arquillian.xml file
<?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"
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="test" transaction-type="JTA">
<jta-data-source>testDatabase</jta-data-source>
<class>za.co.registry.client.login.Credentials</class>
<properties>
<property name="openejb.jpa.init-entitymanager" value="true" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>
The arquillian.xml, I'm setting the testDatabase persistence unit properties in the file. See below under properties.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<container qualifier="openejb-embedded" default="true">
<configuration>
<property name="httpPort">-1</property>
<property name="stopPort">-1</property>
<property name="dir">target/apache-tomee-remote</property>
<property name="appWorkingDir">target/arquillian-test-working-dir</property>
<property name="properties">
testDatabase = new://Resource?type=DataSource
testDatabase.JdbcUrl = jdbc:hsqldb:mem:my-datasource
</property>
</configuration>
</container>
</arquillian>
Configuring my ServiceTest.java file to include the new persistence.xml file.
webArchive.addAsWebInfResource("META-INF/test-persistence.xml", "persistence.xml");
webArchive.addAsResource("META-INF/beans.xml");
Now I can run the findCredentialsByUsernameTest method.
#Test
public void findCredentialsByUsernameTest() {
//create credentials first
Credentials newCredentials = loginService.newCredentials(new Credentials("john", "password"));
//search for credentails
Credentials login = loginService.findByUsername("john");
Assert.assertNotNull(login);
Assert.assertEquals(newCredentials.getUsername(), login.getUsername());
}

Related

OpenJPA Exception related to JDBC Driver and connection URL in TomEE+

We are facing issue in our application with respect to the persistence configuration. The application runs on TomEE Plus (7.0) and uses Open JPA implementation. Below is the error which we are getting
<openjpa-2.4.2-r422266:1777108 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: There were errors initializing your configuration: <openjpa-2.4.2-r422266:1777108 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "oracle.jdbc.xa.client.OracleXADataSource" and URL "jdbc:oracle:thin:#//mydburl.abc.com:1881/MYSID.ABC.COM". You may have specified an invalid URL.
We dont' get the above error when using oracle.jdbc.driver.OracleDriver. The XA driver settings were working fine on IBM WebSphere 8.5. Is there anything else which we need to configure in TomEE+ to get it working?
Update
Adding the persistence.xml file as well
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="MySchema" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>myjndi</jta-data-source>
<class>com.abc.jpa.entity.Tuser</class>
<class>com.abc.jpa.entity.Taddress</class>
<properties>
<property name="tomee.jpa.factory.lazy" value="true"></property>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.xa.client.OracleXADataSource" />
<property name="javax.persistence.jdbc.url"
value="jdbc:oracle:thin:#//mydburl.abc.com:1881/MYSID.ABC.COM" />
<property name="javax.persistence.jdbc.user" value="userName" />
<property name="javax.persistence.jdbc.password" value="password" />
</properties>
</persistence-unit>
</persistence>
As mentioned, it works fine if I use oracle.jdbc.driver.OracleDriver as the jdbc driver class

database access from a jar inside ear

In an ear-file I have a persistence-unit for managing entities inside that ear which works fine. Additionally, there is a commons jar-file located in APP-INF/lib which is a dependency in other ears as well (other ear-modules on the same level). In this commons-jar I would like to have a single entity and create the possibility for CRUD on that entity. Now it is a jar only with an additional persistence unit (but same db as for the ear) and I can't manage to set it all up. Would that persistence unit be managed as well by the container? Could I use EJBs inside that jar file as well? Is it possible at all to do what I want?
I created the entity as follows
#Entity
public class ConfigEntity {
#Id
private Long id;
#Version
private Long version;
[further fields, getters and setters omitted]
}
With the following "Dao" I try to call isEntityManagerNull from a bean inside the ear.
public class ConfigBEDao {
#PersistenceContext(unitName = "configurationPU")
EntityManager entityManager;
public boolean isEntityManagerNull() {
return entityManager == null;
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
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="configurationPU" transaction-type="RESOURCE_LOCAL">
<description/>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/db</jta-data-source>
<properties>
<property name="eclipselink.target-database" value="${database.dialect}"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.cache.shared.default" value="false"/>
</properties>
</persistence-unit>
</persistence>
From a bean inside the ear file I call
ConfigBEDao dao = new ConfigBEDao();
dao.isEntityManagerNull();
which gives me the following exception already when trying to deploy on the server (payara):
java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [ConfigBEDao/entityManager] in the scope of the module called [main_ear-1.0-SNAPSHOT#main_ejb-1.0-SNAPSHOT.jar]. Please verify your application.
at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:733)
at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.findReferencedPUs(EjbBundleDescriptorImpl.java:889)
at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:186)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
Then I changed the persistence.xml as follows but that didn't work either although the application can be deployed:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
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="configurationPU" transaction-type="RESOURCE_LOCAL">
<description/>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/db"/>
<property name="javax.persistence.jdbc.user" value="db"/>
<property name="javax.persistence.jdbc.password" value="secret"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
</properties>
</persistence-unit>
</persistence>
which gave me
javax.persistence.PersistenceException: No Persistence provider for EntityManager named configurationPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)

No transaction is currently active under the eclipselink and jboss

Server: Jboss EAP6.2
JPA: eclipselink 2.4.*
transaction-type="JTA"
get error message:
Exception Description: No transaction is currently active
...
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:189)...
...[some ejb]$$view[some number].[some method](Unknow Source)
...
You will need add in the configuration file persistence.xml the next entry
<property name="eclipselink.target-server" value="JBoss" />
Example complete:
<?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="SQLServer" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/SQLServer</jta-data-source>
<properties>
<property name="eclipselink.target-server" value="JBoss" />
</properties>
</persistence-unit>
</persistence>
<property name="eclipselink.target-server" value="JBoss" />
this config can solved that error

Error No Script Specified when using JPA Persistence Schema Generation

Running on Wildly 8.2.0.Final. I get the following error when trying to generate a database schema from a script.
Caused by: javax.persistence.PersistenceException: Schema generation configuration indicated to include CREATE scripts, but no script was specified
<?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="com.mycompany_mavenFlowChartDB5_war_1.0-SNAPSHOTPU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/postgresql</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name=""/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
<property name="javax.persistence.schema-generation.scripts.action" value="none"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="META-INF/create-script.sql"/>
</properties>
</persistence-unit>
</persistence>
Where the create script is located:
/src/main/resources/META-INF/create-script.sql
Thanks in advance!
The Wildfly persistence schema generator gui creates incorrect property names for the drop and create targets.
The correct property names are
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create-script.sql"/>
change create-script to create-script-source and drop-script to drop-script-source
source: https://github.com/hantsy/ee7-sandbox

JPA- No Persistence provider for EntityManager for MSAccess DB

Im trying to use JPA to connect to a MS access DB using a JDBC:ODBC connection. Here is my Persistance.xml code :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="DSRJPA">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>com.entity.AccessEntity</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:odbc:MSAccessDB"/>
<property name="toplink.jdbc.user" value="admin"/>
<property name="toplink.jdbc.driver" value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<property name="toplink.jdbc.password" value="admin"/>
<property name="toplink.jdbc.read-connections.min" value="1"/>
<property name="toplink.jdbc.write-connections.min" value="1"/>
</properties>
</persistence-unit>
</persistence>
But when i run the code, i get this error :
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named DSRJPA: The following providers:
oracle.toplink.essentials.PersistenceProvider
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
Returned null to createEntityManagerFactory.
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at com.test.Access.main(Access.java:19)
Please throw me some light on this error.
Thanks in Advance.
Have you referenced your persistence unit in the class?
i.e.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("DSRJPA");