Postgres and JPA in macOS - postgresql

I had a windows environment with Postgres 9.6 and Wildfly 14. I have created connections via the wildfly as following:
<datasource jndi-name="java:jboss/datasources/mydb" pool-name="mydb" enabled="true">
<connection-url>jdbc:postgresql://localhost:5432/mydb</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>
<driver>postgresql-42.1.1.jar</driver>
<security>
<user-name>someusername</user-name>
<password>somepassword</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
</validation>
</datasource>
And then my Persistence.xml as following:
<?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="SomePU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/mydb</jta-data-source>
</persistence-unit>
</persistence>
My JPA Entity looks like this:
#Entity #Table(name="usr_apps")
public class SomeApp{...}
My EJB Stateless class:
#PersistenceContext(unitName = "SomePU")
private EntityManager em;
public List getSometing(){
return em.createQuery("select b from SomeApp b").getResultList();
}
The same configs were working on Linux Ubuntu, Linux RHEL, Mac OSX Lion.
but now that I'm trying to run it on macOS mojave, I'm receiving this error when trying to read data through JPA.
[org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) ERROR: relation "usr_apps" does not exist
Position: 150
I'm pretty sure that table exists. I can see the same query running in any other DBMS. I have dumped the database from the my other environment and restored it on the new one.
PS. I also tried PSQL 10. Same result. No data is being read from the database. I can successfully ping the connection from Wildfly management console.
I also made sure that the table is in public schema.
EDIT:
I realized that even though the connection is set to mydb, but JPA is only sending the query to postgres default database!
EDIT 2
Ok this is getting closer to a bug to me.
<datasource jndi-name="java:jboss/datasources/mydb" pool-name="mydb">
<connection-url>jdbc:postgresql://localhost:5432/mydb</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>
<driver>postgresql-42.2.5.jar</driver>
<security>
<user-name>testuser</user-name>
<password>testpassword</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
</validation>
</datasource>
I'm receiving this error:
Caused by: org.postgresql.util.PSQLException: FATAL: database "testuser" does not exist
Why JPA is treating my username as a database name?
PS. WILDFLY 14.0.1.Final

OK!! All I had to do is to remove the datasource class from the connection :
<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>
Finally got it working
Note that wildfly version 14 and onwards adds this line to the xml file automatically when you create the datasource through admin console.

Related

Usage of Hibernate OGM with cloud MongoDB Atlas M0 (Free Tier)

I am trying to use MongoDB Atlas M0 (Free Tier) for my JAVA EE application, now I am using:
Local MongoDB database (v4.0.4)
Hibernate Core "hibernate-core 5.3.6.Final"
Hibernate OGM "hibernate-ogm-mongodb 5.3.1.Final"
Java application server WildFly 15.0.0.Final.
With a local database a pair MongoDB and Hibernate OGM works like a charm, but when I tried to connect Hibernate with Mongo Atlas on free tier to test cloud database, I am was not able to have a working connection, because mongodb driver throws an exception com.mongodb.MongoSocketReadException: Prematurely reached end of stream
I will provide two versions of my persistence.xml, first is working fine with localhost and second is what I used to connect to cloud.
working localhost version:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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_2.xsd"
version="2.2">
<!-- ### MongoDB HIBERNATE OGM PERSISTENCE UNIT ### -->
<persistence-unit name="PersistenceUnitNoSQL" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>org.companyname.model.UserEntity</class>
<class>org.companyname.model.ItemEntity</class>
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb"/>
<property name="hibernate.ogm.datastore.host" value="localhost:27017"/>
<property name="hibernate.ogm.datastore.database" value="databasename"/>
<property name="hibernate.ogm.datastore.create_database" value="true"/>
</properties>
</persistence-unit>
</persistence>
not working atlas cloud version:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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_2.xsd"
version="2.2">
<!-- ### MongoDB HIBERNATE OGM PERSISTENCE UNIT ### -->
<persistence-unit name="PersistenceUnitNoSQL" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>org.companyname.model.UserEntity</class>
<class>org.companyname.model.ItemEntity</class>
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb"/>
<property name="hibernate.ogm.datastore.host" value="cluster0-clustername-shard-00-00-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-01-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017"/>
<property name="hibernate.ogm.datastore.database" value="databasename"/>
<property name="hibernate.ogm.datastore.create_database" value="true"/>
<property name="hibernate.ogm.datastore.username" value="atlas-user-name"/>
<property name="hibernate.ogm.datastore.password" value="atlas-user-password"/>
<property name="hibernate.ogm.mongodb.authentication_mechanism" value="SCRAM_SHA_1"/>
</properties>
</persistence-unit>
</persistence>
Like a host I used replica sets URIs from "standard connection string" from helping window in the Atlas account, because with "short SRV connection string" Hibernate throws org.hibernate.service.spi.ServiceException: OGM000072: Unable to configure datastore provider, so I think there is no support of this type of connection yet.
So with this last persistence.xml configuration I had following error:
20:02:01,094 INFO [org.mongodb.driver.cluster] (ServerService Thread Pool -- 78) Cluster description not yet available. Waiting for 30000 ms before timing out
20:02:01,175 INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='5c5497a97aea6111622c7540', description='null'}-cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017)
Exception in monitor thread while connecting to server cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017: com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112)
at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:570)
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:441)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83)
at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:106)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:63)
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.lang.Thread.run(Thread.java:748)
Hibernate tries to connect to each shard (00-00, 00-01, 00-02), but with all throws this exception.
What I tried to do to fix the problem:
using mongo-java-driver version 3.9.1 instead of Hibernate 3.6.3 built-in, but both drivers work with the same problem
my IP is added (my app is deployed from my laptop) to my atlas account IP Whitelist
I am able to connect fine to cluster from Mongo Shell and MongoDB Compass
I had a doubts about "hibernate.ogm.mongodb.authentication_mechanism", but "SCRAM_SHA_1" and "BEST" didn't worked for me
and finally I tried to make a connection to the cluster from Java directly (without Hibernate)
with
MongoClient mongoClient = MongoClients.create("mongodb+srv://atlas-user-name:atlas-user-password#cluster0-clustername-raa4n.mongodb.net/test?retryWrites=true");
or
MongoClient mongoClient = MongoClients.create("mongodb://atlas-user-name:atlas-user-password#cluster0-clustername-shard-00-00-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-01-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-clustername-shard-0&authSource=admin&retryWrites=true");
and both cases worked fine, I was able to make a connection and to use the database without problems.
So my problem is why Hibernate throws this kind of exception?
It's possible that something is not right in the way Hibernate OGM creates the client.
I think the easier way to check this now for you is to override the MongoDBDatastoreProvider and provide an initialized MongoClient.
You can do this by extending MongoDBDatastoreProvider and overriding the method createMongoClient. Something like:
package org.myprojects;
import org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider;
public class MYCustomMongoDBDatastoreProvider extends MongoDBDatastoreProvider {
#Override
protected MongoClient createMongoClient(MongoDBConfiguration config) {
return MongoClients.create(...);
}
}
then use the property OgmProperties.DATASTORE_PROVIDER to use your datastore provider:
hibernate.ogm.datastore.provider = org.myprojects.MYCustomMongoDBDatastoreProvider
In this example I'm setting it in the hibernate.properties file but you can set it where it makes more sense for your project.
EDIT: Additional explanations about the error.
I think the problems is that we are not using the factory to create the mongo client.
EDIT 2: The problem might be caused by a lack of support for SSL.
An issue has been created and contains more information
Got the same problem. I, additionally, enabled SSL and the connection was success:
<property name="hibernate.ogm.mongodb.driver.sslEnabled" value="true"/>

Table not found with H2

My Java EE application cannot find tables. I am using WildFly (as the application server) and H2 (as the DB, in the embedded mode).
The error is:
org.h2.jdbc.JdbcSQLException: Table "MY_TABLE" not found
Look at my table creation:
create table "MY_TABLE" (
-- ...
);
See how my entity is defined:
#Entity
#Table(name = "MY_TABLE")
public class MyTable {
// ...
}
This is how I call JPA (this causes the exception):
#PersistenceContext
private EntityManager entityManager;
// ...
entityManager.find(MyTable.class, 1);
My persistence.xml is:
<persistence-unit name="myapp" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/myappDS</jta-data-source>
</persistence-unit>
And the standalone.xml in my WildFly:
<datasource jndi-name="java:jboss/datasources/myappDS" pool-name="myappDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:~/myapp;SCHEMA=PUBLIC</connection-url>
<driver>h2</driver>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>
Everything looks good so why the exception? Do I need to flush something? Or set schema somewhere?
If I configure another datasource (having the same DB structure) in WildFly (for example Postgres), everything works fine. That would mean that the datasource configuration is the place causing the error.
(Yes, I am totally sure the DB is not empty and the connection URL is correct as I have tried it from an SQL client.)
Are you running the application as a different user than you are testing to connect with? In that case the the ~ in the connection path will resolve to different home folders, and thus different databases.
Otherwise I would suggest connecting with the Shell in the h2 jar file and run show tables to verify that the table exists and with the correct casing. Start the shell by running:
java -cp h2*.jar org.h2.tools.Shell
Where do you store your script for the table creation? Is it in classpath of the application?
In my test setup I let hibernate generate the tables on startup (and dropping it on shutdown) and using an import.sql script for the test-data generation, which is in the folder src/main/resources.
<persistence-unit name="myapp" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/myappDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>

Database configuration with WildFly

I intend to configure postgresql in my app, but I don't want to manage access to my db through WildFly. Is it possible to avoid this approach and to chose another way but with jndi?
If you want to connect a postgresql database without abstraction layer such as wildFly, you can use simply jdbc (https://jdbc.postgresql.org/about/about.html) :
PostgreSQL JDBC Driver (PgJDBC for short) allows Java programs to
connect to a PostgreSQL database using standard, database independent
Java code. Is an open source JDBC driver written in Pure Java (Type
4), and communicates in the PostgreSQL native network protocol.
If you prefer a lightweight ORM (Object Relational Mapping), I would advise activejdbc (http://javalite.io/activejdbc).
Here you can find the configuration of Wildfly Wildfly datasource configuration . The datasource can be access with jndi. You have to replace the example-jdbc-driver with postgres driver.
Bellow is my example of configuration of datasource for IBM-informix
<datasource jndi-name="java:jboss/datasources/mvpdb" pool-name="mvpdbpool">
<connection-url>jdbc:informix-sqli://mars.bza-intern.de:30020/demomvp:informixserver=tcpmars;DB_LOCALE=de_DE.8859-1;IFX_LOCK_MODE_WAIT=10;</connection-url>
<driver>informix</driver>
<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
<pool>
<min-pool-size>1</min-pool-size>
<max-pool-size>3</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>abcd</user-name>
<password>efgh</password>
</security>
<statement>
<prepared-statement-cache-size>32</prepared-statement-cache-size>
<share-prepared-statements>true</share-prepared-statements>
</statement>
</datasource>
The datasource can be access ( Spring context ):
with Java Configuration
#Bean(name = "mvpds", autowire = Autowire.BY_NAME)
public DataSource Mvpds() {
logger.info("Mvpds-bean");
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource ds = dsLookup.getDataSource("java:jboss/datasources/mvpdb");
return ds;
}
or XML definition
<bean id="mvpds" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>java:jboss/datasources/mvpdb</value></property>
</bean>
The question was not correct because of lack of knowledge. The promblem is that no way to configure db without WildFly but with jndi. Jndi is an instrument WildFly uses to find a datasourse:)

How to create tables from entity classes in JPA on server Startup?

I am supposed to check for existence of tables on server startup. If they did not exist I have to create them using entity classes. Is this even possible?
I am using Eclipse and my server is wildfly10. I am connecting to Oracle 11g xe but I don't think it is database that is causing issues
Anyway, this is what I have done so far
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="Lab5">
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Without above property my database query throws Exception. But what is really strange, is that with this property query returns empty list, but table does not exist in database.
Also, I noticed warning showing on server startup
HHH000431: Unable to determine H2 database version, certain features may not work
I tried various fixes for this warning found on stackoverflow and other sites but they did not help. I don't even know if this is related to my problem
JPA 2.1 made DDL generation a part of the specification. You can use the "javax.persistence.schema-generation.database.action" persistence property with a "create" value to have 2.1 providers create the database schema for you during deployment. JPA can be used to generate or even run custom scripts should you need to modify the table creation/tear down and population process.
The problem you are likely encountering is your persistence unit does not specify a datasource, leaving it up to the container to figure out which you want to connect to, and Wildfly must be defaulting to an H2 database. The tables will be setup in this database, but not the Oracle XE database you are expecting - which is why queries return no values and yet in your console, the tables don't even exist.
You need to setup a datasource to your database in the server, and then point your persistence unit to it using the <non-jta-data-source> or <jta-data-source>. Your persistence.xml is incomplete, so I would urge you to look at a demo persistence unit first.
This will likely depend on the implementation you're including with your project. Assuming you're using hibernate for persistence, you'll need to add the following:
...
<persistence-unit ...>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
...
<properties>
<property name = "hibernate.hbm2ddl.auto" value="create" />
...
</properties>
</persistence-unit>
Note that this will assume that your connection is formed with a user having the required privileges in the db for table creation. Take a look at the hibernate docs here.

In JBoss/WildFly should I enable JTA on data source to use with JPA?

In JBoss/WildFly, when configuring a data source, there is a JTA option, which is disabled by default:
<datasource jta="false" jndi-name="java:/wt/testds" pool-name="testds" enabled="true" use-ccm="false">
...
</datasource>
Now I want to associate this data source with JPA using JTA transaction type:
<?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="test" transaction-type="JTA">
<jta-data-source>java:/wt/testds</jta-data-source>
</persistence-unit>
</persistence>
Do I also need to enable JTA on the data source?
Yes, of course you need to enable JTA on a datasource if you want to have jta transactions!
Your XML/JBoss/Wildfly config file will look like this:
<datasource jta="true" ...
In our webapp persistence-unit, the datasource looks like this:
<jta-data-source>java:jboss/datasources/CoreDS</jta-data-source>
The transaction-type="JTA" isn't necessary, at least not in my setup (Wildfly 8.1).
In your Java code you can go like this to use transactions:
#TransactionManagement(TransactionManagementType.CONTAINER) // class level
public class ...
...
#PersistenceContext(unitName = "CoreJPA")
EntityManager em;
#Resource
private EJBContext ejbContext;
...
#TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) // method level
public void doSomething(...)
And if you need to rollback, you do this:
try {
...
} catch (Throwable t) {
log.error("Exception in create work order: " + t.getMessage());
ejbContext.setRollbackOnly();
throw t;
}
There are a lot of resources about this that can be found using Google.
I've just experienced a problem related to this issue.
I was running a container managed transaction involving approximately 20,000 inserts into a MySQL database.
The transaction failed randomly, sometimes after around 3,500 inserts, other times after around 6,000 inserts, etc.
After investigation I found that the JTA option on the WildFly datasource definition was set to false.
Changing this setting to true fixed the problem, so I would agree with #user3472929 that JTA should be set to true in the datasource definition unless you have some specific reason not to.
I think you should use jta. And if you set jta to false in the container configuration file, jta will be disabled for JPA, so the transaction-type for JPA will be "RESOURCE_LOCAL", which has some nasty side effect. By the way, jta is true in the container configuration file by default.