I'm implementing database access to a Postgres database using WebLogic 12c, JPA 2.2 and Hibernate 5.
I finally solved the problem, where I couldn't make 2 separate request from the database with separate datasources, users, etc. by switching Global Transactions off.
Now my problem is that whatever I do, the entities I'd like to save are not actually saved to the database. I have the #Transactional on all methods where inserting happens, but nothing gets written to the DB.
I went ahead and created an interceptor for Hibernate, which logs out when and what transaction gets commited. I see that something gets commited, but the transaction I get from the method parameter is null.
I'm sure I'm overlooking something obvious, but I can't for the life of me get to know what it is
Related
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.
I have a spring boot application with java 8 ,jpa etc and a jboss application with j2ee applications which calls too many sql procedures to update the table.
I have a query something like this in spring boot to get all the employee:
#Cacheable("employeeList")
List{Employee} findByAddressId(Long addressId);
But if someone inserts a new record to Employee table in the same address id from sql procdure from jboss application, the spring boot application is not able to pick the new records , because the query is so generic to that address id.
So i want to create a trigger on that table on insert and update , so when ever insert/update happens it should update the cache with new records belongs to that address id.
Can somebody please tell me how to do this?
If I understand the question correctly you have a spring boot app and a separate jboss app that are connecting to the same database and are insert/updating to the same database tables.
With spring's #Cachable you need to be able to tell spring when you should evict the cached item. For example, having the method that updates the entity being marked as #CacheEvict is an easy way to evict the entity from the cache. The problem here is that if the jboss app updates a record there is no way for spring boot app to know this.
Using a database trigger would seem problematic since you'd have to somehow have the db trigger communicate to the spring boot app to allow eviction to happen.
One solution may be having both the jboss and spring boot app use a distributed caches, like ehcache with terracotta.
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.
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
I have a struts 2 application and a toplink persistence provider running on tomcat 6.0.20 and a MySql 5.1.38 server on a GNU/Linux machine. After committing the data the when i go to retrieve it the data it has disappeared from the database.
I do a em.commit() and em.flush() after my queries have executed. How do they disappear? I am using all standard configuration files. I have reduced the wait_timeout and the interactive_timout period in mysql. Also am using autoReconnectforPools in my persistence.xml.
I also invalidate the cache on every users logout.
Any ideas?
anyway it does not matter, the problem was solved by removing softweak from persistence.xml's entity type declaration and adding hardweak in its place.