0
votes

My currect project includes, JPA2.0 on Hibernate platform, HSQLDB2.0 and JBoss5.1 .

I have also defined datasource in JBoss under the name hmc.

Persistence.xml looks like : <persistence-unit name=HMC><jta-data-source>java:/hmc</jta-data-source>...</persistence-unit>

Can anybody let me know

  1. what is the difference between jta and non-jta datasource definition in JPA persistence.xml.

  2. How to get connection, start a transaction in JTA mode?

I am bit confused as how to start a transaction for persisting the entities.

EntityManager works whenever select query is run, but doesnt work for persisting the data.

I tried

  1. with EntityManager, it threw IllegalStateException : A JTA EntityManager cannot use getTransaction() exception.

  2. with lookup of the datasource and was able to get connection object, but could not start transaction from that javax.sql.Connection object.

  3. with @Inject, @Resource for EJBContext, EntityManager and UserTransaction.

Regards,

Satya

2

2 Answers

2
votes

For the differences between the JTA and non-JTA, one could refer to this article

For how to access the JDBC Connection in JTA mode, you could refer to this article

with EntityManager, it threw IllegalStateException : A JTA EntityManager cannot use getTransaction() exception.

I think this error occurs because the container is the one managing your transaction, not you (otherwise it'll be a non-jta and you'll be using application-managed-entityManager)

The reason you cannot access the transaction is that you can then call on commit or other methods that should be done by the Java EE container.

with lookup of the datasource and was able to get connection object, but could not start transaction from that javax.sql.Connection object.

I think this is great already since you can already get the connection from the datasource, which is the clean way to do it. I dont think you need to worry about the transaction here since it's already managed by the Java EE container you're using. So why dont just do the stuffs with the connection already ? :-)

0
votes

Mmm.. What about Batch processing.. We are quite limited!

http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/batch.html

I need the session, I need the transaction.