Java EclipseLink EntityManger: Way to Ensure Connection Can be Established? - java

I'm using eclipselink 2.4 in a JAX-RS (Jersey) application.
In my base controller, I connect to my database, but since the entity manager only really connects when a query is executed, an exception is thrown.
Is there a way I could determine if I can connect to a database after I get the EntityManager object so that I can handle the exception myself (and fail over manually to another database).
EDIT: assume I cannot change the underlying DBMS at all.

You could create a ServletContextListener and run a query on startup for your application. However if it fails you are out of luck for the error handling I guess.
You should really get a clustered HA database.

Related

MySQL "No database selected" over jdbc

We have a Java application (basically integration tests) that uses Hibernate (which uses Jdbc) to read/write data to the MySQL Database. Hibernate objects like sessions or transactions are created and configured via our own code (no Spring or other wrappers are being used). The issue is that periodically (multiple times during tests execution) we observe a "No database selected" exception. Database URL that we use for DataSource configuration already contains database name in it:
jdbc:mysql://localhost:3306/test?useSSL=false&createDatabaseIfNotExist=false&cacheServerConfiguration=true&cacheResultSetMetadata=true&useLocalSessionState=true&rewriteBatchedStatements=true&tcpNoDelay=true&tcpTrafficClass=16&alwaysSendSetIsolation=false&tcpSndBuf=1048576&tcpRcvBuf=1048576&characterEncoding=utf8&allowPublicKeyRetrieval=true
I tried to catch the Exception and test the connection's selected database by running select database() and it actually reports that the value is null on the database side.
Even more strange thing is that next queries on the same connection are executed against the normal database (so it somehow self-heals).
Does anybody know why can MySQL connections "lose" and then "restore" selected database?
Or maybe there is a way to trace the problem down. Will be grateful for any help or thought that you can provide
Versions:
Java 1.8.0_292
Mysql 5.6.31
Hibernate 5.4.2
JDBC mysql-connector-java 8.0.22

Hibernate session persisting between requests

I'm working on a system where things can and will change outside of JPA, so I need a new session for every request, but my JavaEE app deployed into TomEE persists sessions between requests, resulting in entities that are cached when they've since been updated somewhere outside of the app.
I attempted to create a cfg.xml and get the session factory that way, but was just met with exceptions. I also attempted to unwrap the entity manager class to get the factory that way, but got an exception saying the class couldn't be unwrapped. I feel like this may be something to do with how TomEE and Hibernate interact. Are there issues with my current setup. Or am I trying to implement session-per-request wrong,
The problem that you have is not with the session, it is related with the session cache, so what you can do is invalidate your session cache:
session.refresh(entity)
then hibernate will compare database data and entity object data if there are differences then it will execute again an sql query.
session.clear()
will remove everything from the session cache, so you will not get old data.

How to start up a springboot service when DB2 connection is down

My springboot microservice depends on AS400 DB2 which may be down when the service starts up.
The service has a configuration bean which has autowired JpaRepository based repositories.
During startup, when DB2 is down I get following message:
"org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set"
So the services fails to start up and remains dead till forcefully rebooted.
If I set JVM parameter "-Dhibernate.dialect=org.hibernate.dialect.DB2400Dialect", the service starts up and I can make requests to the service when DB2 comes up. It feels like a hack, so I wonder if there is the right way to deal with this issue for springboot services?
I saw this thread, but it doesn't answer my question.
Basically what I am looking for is somehow start the microservice with those autowired repositories even if DB is down at the moment so that later, when the actual request comes, the connection to the DB could be restored and request serviced.
Have you tried to specify Hibernate dialect for your DBMS flavour in src/main/resources/application.properties like:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2400Dialect
?
Spring Boot autodetects dialect on startup if database is up but there is nothing inappropriate to specify your dialect explicitly in source code.

How can an Hibernate web application start if its database is not yet available

What would be the best way to setup/design or simply configure an Hibernate based Java web application to support being started (i.e. sessionfactory initialization) up if the database connectivity is not yet available, but will be, albeit at a much later time.
In other words, is there an easy way to handle out of order initialization between an Hibernate server application and its database?
As far as i know . If you use external connection pool and hibernate is no responsible to making the connections and in additional hbm2ddl is set to none than hibernate should not connect to the database untill you open a session.
Any way if it will failed to open session because there is no connection it will success to open new session as soon as there is databas connectivity.

can you create your own JTA transaction?

I am loading a large set of data into a database from a webservice. I am using eclipslink for persistence and running the application on glassfish 3.0. I run into problems on my test data set in that there are a few foreign key constraint violations. I am fine with the violation, I do not want that data if it is not complete. My problem however comes in that the exception is thrown in the container. That then marks my transaction for a rollback, and I get no data at all then.
I would like to continue using JTA but am not sure if I can do what I want to achieve, and that is create my own JTA transaction so I can control when it commits,etc. I am not sure if that is a good idea though as I feel by doing so I may be destroying some of the benefits of using JTA.
So is it possible to get a JTA transaction?
Do the database work in a method of a session bean. Annotate that method with:
#TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
It will be given its own transaction. The outer transaction will be suspended while it does its stuff.
http://wiki.eclipse.org/Using_Advanced_Unit_of_Work_API_%28ELUG%29#Integrating_the_Unit_of_Work_with_an_External_Transaction_Servicestrong text**
Read How to Acquire a Unit of Work with an External Transaction Service. Apparently you can snatch the UserTransaction and/or start your own by querying the container JNDI for UserTransaction

Categories

Resources