Java JPA, setting transaction isolation level using an entity manager - java

I'm trying to set a transaction isolation level for the connection associated with a given entity manager (TRANSACTION_SERIALIZABLE etc.).
I have scoured the internet for a solution and found a few. However, none of them seem to work.
When I try to do the following :
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("BankingPU");
public EntityManager em = emfactory.createEntityManager();
java.sql.Connection connection = (java.sql.Connection) em.getDelegate();
connection.setTransactionIsolation(TRANSACTION_SERIALIZABLE);
I get the following exception:
Exception in thread "main" java.lang.ClassCastException:
org.eclipse.persistence.internal.jpa.EntityManagerImpl cannot be cast to java.sql.Connection
When I do
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("BankingPU");
public EntityManager em = emfactory.createEntityManager();
java.sql.Connection connection = em.unwrap(java.sql.Connection.class);
The value stored to connection is null.
I ran the following in order to find out more:
Object obj = em.getDelegate();
The class type stored in obj is org.eclipse.persistence.internal.jpa.EntityManagerImpl
Edit: (oops, guess I could have told that from the exception)
Edit2:
I have managed to obtain the session (i think) by calling :
Session session = ((EntityManagerImpl) em).getSession();
However, neither the connection(), nor the DoWork() methods are present in it.
Any idea how to follow up on this and set the transaction isolation level?

JPA API does not have transaction (TX) isolation level management API. So it is not possible do it via entity manager.
If you want to use the same entity manager than you need, as it already mentioned in Neil Stockton comment, your JPA provider specific code to get JDBC access for changing TX isolation level.
Adam Bien almost answered on your question at his blog post.
Isolation Levels can be set on java.sql.Connection level. In a Java EE environment isolation levels can be configured on Data Source / Connection Pool (see e.g. Glassfish).
As workaround, if you want use only JPA API, you may create in your application server the several data sources for the same database with required isolation level.
However you need to understand that you will have the several 1st level caches (in different entity managers) and you should manage your entities state in appropriate way.

I have found the solution:
emfactory = Persistence.createEntityManagerFactory("BankingPU");
em = emfactory.createEntityManager();
Session session = ((EntityManagerImpl) em).getSession();
DatabaseLogin databaseLogin = (DatabaseLogin) session.getDatasourceLogin();
databaseLogin.setTransactionIsolation(DatabaseLogin.TRANSACTION_SERIALIZABLE);

Related

JPA commit() after persist() : Required or Not

I am working an a JPA 2.0 project, where I am saving my Entity class objects like :-
InitialContext ctx = new InitialContext();
UserTransaction userTrans = (UserTransaction)
ctx.lookup("java:comp/UserTransaction");
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_NAME);
EntityManager em = emf.createEntityManager();
User user = new User("ankit","nigam",25);
em.persist(user); // persisted in db after this executes
userTrans.commit(); // whether it is required OR not.
So whether I am using userTrans.commit() or not, my user object is getting saved in Db, after persist() executes. But some of my colleagues say, as a standard we should commit() the transaction.
What should be the approach which I follow and whats the logic behind commit() and persist(). Please throw some lights.
Is autocommit ON in your DB? If it is then that is the reason why the changes get permanently stored in your DB irrespective of whether or not you commit the transaction from your application. In production the autocommit is generally set OFF because it hampers the performance/response time of the DB, that is why developers are generally encouraged to control the commit or rollback of a transaction from their application. The link details the command to handle autocommit in db2: http://www.db2util.com/administration/options-db2-command-line-disable-autocommit/

Why can I insert/update data with hibernate even when there's no transaction involved?

As we all know that in Hibernate if no transaction commit, the changes won't affect in database. But I found something weird. And the code as follows:
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring.xml");
SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Model model = new Model();
...
session.save(model);
session.flush();
session.close();
And the model was saved to database even there's no transaction, anyone can explain this?
Any comments would be appreciated! Thanks!
PS: I am using mysql.
The session.flush command saved the transaction. If it's wrong, you should use transaction.
usually hibernate needs the line session.beginTransaction(); to work. You didn't write that and your application worked, I guess your application runs in an Application server, which provides transaction management. e.g. jboss, weblogic...
However it doesn't mean that there is no transaction. Did you set auto-commit true?
btw, session.flush() and txn.commit() are different.
Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.
After session.flush(), you still can call txn.rollback() to rollback all changes.
edit
oh I saw you used spring. did you configured txnmanager in spring?
Hibernate doesn't need transactions, the most common problems in database-based applications are just easier to solve with transactions which is why usually everyone uses transactions with Hibernate. But that's mere coincidence/convention/laziness.
All Hibernate needs is a java.sql.Connection and if your container provides one even though there is no current transaction manager configured, Hibernate is fine with that.
In fact, Hibernate has no idea that there might be a transaction manager. So session.flush() will use the ApplicationContext to get a connection, generate the SQL and use JDBC to send the generated SQL code to the database.
From Hibernate's point of view, that's all that happens.
There can be several reasons why the data is committed to the database:
You forgot to turn of auto commit on the connection.
Your web container / spring config automatically wires a transaction manager that synchronizes with HTTP requests.
Your code is called form another method which is annotated with #Transactional; in this case, you inherit the existing transaction.

"suspend" hibernate session managed by spring transaction manager

Is there any way to remove/suspend a current spring managed hibernate session from a thread so a new one can be used, to then place the original session back onto the thread? Both are working on the same datasource.
To describe the problem in more detail. I'm trying to create a plugin for a tool who has it's own spring hibernate transaction management. In this plugin I would like to do some of my own database stuff which is done on our own spring transaction manager. When I currently try to perform the database actions our transaction manager starts complaining about an incompatibly transactionmanager already being used
org.springframework.transaction.IllegalTransactionStateException: Pre-bound JDBC Connection found! HibernateTransactionManager does not support running within DataSourceTransactionManager if told to manage the DataSource itself. It is recommended to use a single HibernateTransactionManager for all transactions on a single DataSource, no matter whether Hibernate or JDBC access.
A workaround that seems to do the trick is running my own code in a different thread and waiting for it to complete before I continue with the rest of the code.
Is there a better way then that, seems a bit stupid/overkill? Some way to suspend the current hibernate session, then open a new one and afterworths restoring the original session.
Is there any reason you can't have the current transaction manager injected into your plugin code? Two tx managers sounds like too many cooks in the kitchen. If you have it injected, then you should be able to require a new session before doing your work using the #transactional annotation's propagation REQUIRES_NEW attribute see the documentation for an example set-up
e.g.
#transactional(propogation = Propogation.REQUIRES_NEW)
public void addXXX(Some class) {
...
}
But this would use spring's PlatformTransactionManager rather than leaving it up to hibernate to manage the session / transaction.

Working with JDBC in EJB-Hibernate env

I have a little of confusion about JDBC connection, transactions and their integration in EJB, JTA, Hibernate environment. My doubts are:
when we use #Resource DataSource ds; ... ds.getConnection() , are we working in the same transaction used by the managed bean? Should we close the connection, statement, resultset?
what about session.doWork? Are we in the same transaction? What about closing statement and result set?
aggressive release mode in Hibernate means that connections are closed after each statement. Does it mean that transaction is committed too? (I don't think this is true, but I can't understand how Hibernate works here)
There are a few things you need to figure out. First thing you need to identify what is your unit of work.
The session-per-request pattern is one of the most used and unless you have specific needs stick with that.
If you are using Hibernate you don't use statements and result sets directly. Hibernate will do that for you. what you need to close is the hibernate session
What you use is a SessionFactory and a Session object. The session pretty much represents your unit of work. Inside the hibernate session you get your objects, you change them and save them back.
The session per request pattern opens a session when a request is received and closes it when the response is sent back.
In a container managed EJB session bean a transaction is available and the datasource you (or hibernate) use in such a container is automatically handled by a JTA TransactionManager.
Now because Hibernate is smart it can automatically bind the "current" Session to the current JTA transaction.
This enables an easy implementation of the session-per-request strategy with the getCurrentSession() method on your SessionFactory:
try {
UserTransaction tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");
tx.begin();
// Do some work
factory.getCurrentSession().load(...);
factory.getCurrentSession().persist(...);
tx.commit();
}
catch (RuntimeException e) {
tx.rollback();
throw e; // or display error message
}
So to answer your questions:
If you are using Hibernate with JTA in a container you'd be better off using a JPA EntityManager or maybe spring hibernate template.
Here are some references:
http://community.jboss.org/wiki/sessionsandtransactions#Transaction_demarcation_with_JTA
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html

HibernateTransactionManager(Spring) with multiple Session Factories

I have a DAO implementation that uses a HibernateTransactionManager for transaction management and the application has 2 session factories. I am getting an exception at the transactionManager.commit() line below. Does performing Hibernate operations within a transaction manager related to a different session factory cause problems?
TransactionStatus status = transactionManager.getTransaction(def);
try{
doHibernateStuff1(); //Does Hibernate stuff with session
//factory related to Tx Manager
doHibernateStuff2(); //Does Hibernate stuff with session
//factory not related to Tx Manager
}
catch(DataAccessException){
transactionManager.rollback(status);
}
transactionManager.commit(status); //Exception happens here.
The exception appears to be trying to perform the operations in doHibernateStuff2(); again in the txManager.commit().
If you want to suggest a kludge and/or proper way of dealing with this, I would appreciate it.
Are you using XA drivers to connect to the two data sources involved in the transaction? Can't work otherwise.
I know it's an old question but I've encountered similar problem. I presume that Brandon has 2 session factories for different data sources and he's using HibernateTransactionManager. And I believe that using such manager is the problem. From what I've read HibernateTransactionManager isn't able to work with 2 different session factories. Instead he should use a different manager like JTA transaction manager. But only if he needs access to both data sources in one transaction. Otherwise the solution should be using extra manager for every session factory like mentioned in the link bellow:
similar problem

Categories

Resources