sync entitymanager from database - java

i have a swing desktop application that use a entitymanagerfactory, but when this application is executed many times at the same time, the diferents entity managers have old data that was modified by others and this changes will not visible until the next entitymanagerfactory... how can i sync in anytime the entitymanager with the database data??

EntityManager instances should not be held for prolonged periods of time; instead each should be used for unit of work and discarded afterwards.
That said, EntityManager has a refresh() method you can invoke to reload state of a particular entity from the database.
It also has a clear() method which will clear the persistence context completely of "old" data. You need to be careful with it, though - calling clear() without flush() will discard all pending updates.

Related

What is a transaction boundary in hibernate

I have 2 questions related to each other
Q1 What exactly is a transaction boundary in hibernate/Spring Data JPA.
I am new to JPA , so please give a very basic example so I can understand as I tried to read multiple blogs but still not very clear.
Q2 And on top of it, what does this mean-
In hibernate, persist() method guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries, save() method does not guarantee the same.
What is outside and inside of a transaction boundary and how executions are performed outside boundaries?
A transaction is a unit of work that is either executed completely or not at all.
Transactions are fairly simple to use in a typical relational database.
You start a transaction by modifying some data. Every modification starts a transaction, you typically can't avoid it. You end the transaction by executing a commit or rollback.
Before your transaction is finished your changes can't be seen in other transactions (there are exceptions, variations and details). If you rollback your transaction all your changes in the database are undone.
If you commit your changes your changes become visible to other transactions, i.e. for other users connected to the same database. Implementations vary among many other things if changes become visible only for new transactions or also for already running transactions.
A transaction in JPA is a database transaction plus additional stuff.
You can start and end a transaction by getting a Transaction object and calling methods on it. But nobody does that anymore since it is error prone. Instead you annotate methods with #Transaction and entering the method will start a transaction and exiting the method will end the transaction.
The details are taken care of by Spring.
The tricky part with JPAs transactions is that within a JPA transaction JPA might (and will) choose to delay or even avoid read and write operations as long as possible. For example when you load an entity, and load it again in the same JPA transaction, JPA won't load it from the database but return the same instance it returned during the first load operation. If you want to learn more about this I recommend looking into JPAs first level cache.
A transaction boundary it's where the transaction starts or is committed/rollbacked.
When a transaction is started, the transaction context is bound to the current thread. So regardless of how many endpoints and channels you have in your Message flow your transaction context will be preserved as long as you are ensuring that the flow continues on the same thread. As soon as you break it by introducing a Pollable Channel or Executor Channel or initiate a new thread manually in some service, the Transactional boundary will be broken as well.
some other people ask about it - look it up.
If you do not understand something write to me again more accurately and I will explain.
I really hope I helped!

java : when can a rollback fail in jpa?

I'm using JPA and I begin a transaction before a function is called which will in turn store some data into the database. The transaction is then ended after the function call is executed. But I also use entityManager.flush() and entityManager.clear() guessing that memory will be freed on using clear(). before ending the transaction. Please do correct me. When I executed the program, it failed to roll back.
I also tried removing the flush() and clear(), still I'm not able to roll back. When can a roll back fail in my scenario when I'm using clear and flush?
I also use entityManager.flush() and entityManager.clear() guessing
that memory will be freed on using clear().
Well you should stop guessing and start reading some documentation.
EntityManager flushes on tx.commit() by itself, and entity manager is cleared when it is closed.
If you are using container manager transactions, you should rather not worry about flushing and clearing entity managers. If you are using JavaSE then your normal workflow with DB should look like this:
Create new entity manager
Start transaction
Do your job - delete/update/insert whatever
Commit transaction / Rollback if needed
Close entity manager
Normally this would be closed in small try-catch-finally block
EntityManager is lightweight and short living component. You should create one just when you need it, and close it right after you use it.
What is supposed to be a singleton instance, and created (most cases) only once is EntityManagerFactory

Do create an EclipseLink EntityManager per method call or guard with a lock?

should I create an EclipseLink EntityManager per method call, store in thread local or guard with a lock?
Obviously it's initially created from entityManagerFactory.createEntityManager().
Which is best practise please?
With per method call I'm concerned with performance.
With thread local I'm concern about cache visibility between threads.
With guarding a single EntityManager with a lock I have the cost of a lock each time.
/I'm using JSE - so no EJBs, no injtection - just entityManagerFactory.createEntityManager() in a multi-threaded JSE app.
Thanks
EntityManagers are not thread safe, and are designed to represent a unit of work. Each method/thread should have its own unless participating in a larger transaction. It is also better to close/clear them at logical points because they maintain a cache of managed entities that can grow large with long lived EntityManagers. There are numerous posts on what the best way to go is outside and inside a container.
EclipseLink's EntityManager uses EclipseLink's native sessions and unitOfWork underneath, which will lazily fetch resources as required, and release them when done. But they can be configured to operate differently.

Running multiple JPA transactions in parallel

I have two (or more) Java Threads creating, updating and deleting entities from a mysql database using JPA. To achieve this I have a PersistenceLayer class creating the EntityManager and providing save, update and delete methods for all my entities looking like:
public void saveEntity(Entity entity) {
manager.getTransaction().begin();
manager.persist(entity);
manager.getTransaction().commit();
}
public void saveEntity2(Entity2 entity) {
manager.getTransaction().begin();
manager.persist(entity);
manager.getTransaction().commit();
}
If one thread enters operation saveEntity and the other saveEntity2 at the same time, both will try to get the transaction from the EntityManager wich will fail. However I always thought that the underlying database is able to manage multiple transactions, especially if both are working on different rows or even different tables. Of course I could synchronize the blocks, but that would mean only one DB connection is possible at a time which will not scale to multiple users creating several threads.
What wrong assumption am I doing here? Is it possible to submit multiple transactions to a database via JPA and let the DB handle concurrency issues?
EntityManager is not intended to be used by multiple threads. You need to obtain separate instances of EntityManager for each thread.
Actually, if you use EJB or Spring you can use a transaction-scoped EntityManager, which can be used from multiple threads (it's a proxy which delegates actual work to separate thread-bound instances of EntityManager), but I think it's not your case.
To handle concurrency issues when the object is being processed accross multiple transactions, lock can be acquired on the object. Either Optimistic or Pessimistic locking can be used with appropriate lock-modes.
Locking the versioned object by entityManager.lock(entity, LockModeType.READ) will ensure that it will prevent any Dirty Read & Non-Repeatable Read.
LockModeType.WRITE will force incrementing/updating the version column of the entity.

What's the use of session.flush() in Hibernate

When we are updating a record, we can use session.flush() with Hibernate. What's the need for flush()?
Flushing the session forces Hibernate to synchronize the in-memory state of the Session with the database (i.e. to write changes to the database). By default, Hibernate will flush changes automatically for you:
before some query executions
when a transaction is committed
Allowing to explicitly flush the Session gives finer control that may be required in some circumstances (to get an ID assigned, to control the size of the Session,...).
As rightly said in above answers, by calling flush() we force hibernate to execute the SQL commands on Database. But do understand that changes are not "committed" yet.
So after doing flush and before doing commit, if you access DB directly (say from SQL prompt) and check the modified rows, you will NOT see the changes.
This is same as opening 2 SQL command sessions. And changes done in 1 session are not visible to others until committed.
I only know that when we call session.flush() our statements are execute in database but not committed.
Suppose we don't call flush() method on session object and if we call commit method it will internally do the work of executing statements on the database and then committing.
commit=flush+commit (in case of functionality)
Thus, I conclude that when we call method flush() on Session object, then it doesn't get commit but hits the database and executes the query and gets rollback too.
In order to commit we use commit() on Transaction object.
Flushing the Session gets the data that is currently in the session synchronized with what is in the database.
More on the Hibernate website:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-flushing
flush() is useful, because there are absolutely no guarantees about when the Session executes the JDBC calls, only the order in which they are executed - except you use flush().
You might use flush to force validation constraints to be realised and detected in a known place rather than when the transaction is committed. It may be that commit gets called implicitly by some framework logic, through declarative logic, the container, or by a template. In this case, any exception thrown may be difficult to catch and handle (it could be too high in the code).
For example, if you save() a new EmailAddress object, which has a unique constraint on the address, you won't get an error until you commit.
Calling flush() forces the row to be inserted, throwing an Exception if there is a duplicate.
However, you will have to roll back the session after the exception.
I would just like to club all the answers given above and also relate Flush() method with Session.save() so as to give more importance
Hibernate save() can be used to save entity to database. We can invoke this method outside a transaction, that’s why I don’t like this method to save data. If we use this without transaction and we have cascading between entities, then only the primary entity gets saved unless we flush the session.
flush(): Forces the session to flush. It is used to synchronize session data with database.
When you call session.flush(), the statements are executed in database but it will not committed.
If you don’t call session.flush() and if you call session.commit() , internally commit() method executes the statement and commits.
So commit()= flush+commit.
So session.flush() just executes the statements in database (but not commits) and statements are NOT IN MEMORY anymore. It just forces the session to flush.
Few important points:
We should avoid save outside transaction boundary, otherwise mapped entities will not be saved causing data inconsistency. It’s very normal to forget flushing the session because it doesn’t throw any exception or warnings.
By default, Hibernate will flush changes automatically for you:
before some query executions
when a transaction is committed
Allowing to explicitly flush the Session gives finer control that may be required in some circumstances (to get an ID assigned, to control the size of the Session)
The flush() method causes Hibernate to flush the session. You can configure Hibernate to use flushing mode for the session by using setFlushMode() method. To get the flush mode for the current session, you can use getFlushMode() method. To check, whether session is dirty, you can use isDirty() method. By default, Hibernate manages flushing of the sessions.
As stated in the documentation:
https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/chapters/flushing/Flushing.html
Flushing
Flushing is the process of synchronizing the state of the persistence
context with the underlying database. The EntityManager and the
Hibernate Session expose a set of methods, through which the
application developer can change the persistent state of an entity.
The persistence context acts as a transactional write-behind cache,
queuing any entity state change. Like any write-behind cache, changes
are first applied in-memory and synchronized with the database during
flush time. The flush operation takes every entity state change and
translates it to an INSERT, UPDATE or DELETE statement.
The flushing strategy is given by the flushMode of the current
running Hibernate Session. Although JPA defines only two flushing
strategies (AUTO and COMMIT), Hibernate has a much
broader spectrum of flush types:
ALWAYS: Flushes the Session before every query;
AUTO: This is the default mode and it flushes the Session only if necessary;
COMMIT: The Session tries to delay the flush until the current Transaction is committed, although it might flush prematurely too;
MANUAL: The Session flushing is delegated to the application, which must call Session.flush() explicitly in order to apply the
persistence context changes.
By default, Hibernate uses the AUTO flush mode which triggers a
flush in the following circumstances:
prior to committing a Transaction;
prior to executing a JPQL/HQL query that overlaps with the queued entity actions;
before executing any native SQL query that has no registered synchronization.
Calling EntityManager#flush does have side-effects. It is conveniently used for entity types with generated ID values (sequence values): such an ID is available only upon synchronization with underlying persistence layer. If this ID is required before the current transaction ends (for logging purposes for instance), flushing the session is required.
With this method you evoke the flush process. This process synchronizes
the state of your database with state of your session by detecting state changes and executing the respective SQL statements.

Categories

Resources