Container managed transaction in HIbernate - java

I have some doubts on container managed transaction, as per my knowledge container managed transaction is one where container takes care of transactions i.e commit and rollback.
What is container? Is it EJB or App server?
If I want to do CMT with hibernate how do Iachieve this? I have tried using app server JTA but when I open session in hibernate it says no transaction found.
If I use App server JTA implementation what kind of transaction is it?
Do I have to make use of EJB to have CMT?

EJB is a container, a container like EJB can be part of an App server.
Try to find some tutorials, there are many of them available. Here an tutorial for EJB on JBoss: http://www.mastertheboss.com/jboss-frameworks/hibernate-jpa/quickstart-tutorials-hibernate-jpa
Sry, i don't understand the question.
CMT is an ejb concept, but you could use the transaction management from spring => have a look at this if you are interested in spring transaction management: http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/transaction.html
Just try some simple tutorials => for example google for ejb hibernate tutorial => after some practice you will find out how it works :)

Related

Is Spring Data's #Transactional(Propagation.REQUIRES_NEW) supported in Vert.x?

From what I have read so far, Propagation.REQUIRES_NEW makes use of transaction suspension capabilities in most common Java EE containers (JBoss, Glassfish, etc.)
However, as we're running Spring Data inside Vert.x, which is container-less, I'd like to find a definitive answer as to whether REQUIRES_NEW is supported in this scenario, or if we will have to use another approach for this.
You can use Spring #Transactional annotations out of a JavaEE container. It will work well if you only use a database.
If you mix transactional components (a relational database and a message broker) then you need a transaction manager, which is often only available in JavaEE container.
I'm not a Spring or SpringBoot expert but I am pretty sure it's not difficult nowadays to add a transaction manager to a Spring/Tomcat project.
As for running Spring Data in Vert.x, it will work fine if you deploy this code in worker verticles. Check out the spring-worker sample in the vertx-examples repository on GitHub

Does Apache Tomcat 6 support JTA?

I have some confusion on using JTA within the Spring Framework on Apache Tomcat and i hope someone will clarify as after many research i can't seem to find the correct answer as of yet.
I am developing a web application using Spring Framework to be running on Apache Tomcat 6.
I read somewhere that Spring's does support for JTA but it delegates to the underlying JavaEE application server. Now this is where i am confused because i Tomcat is not a full JavaEE application server - it is merely a servlet container and as i believe it doesn't provide JTA implementation like the full JavaEE application server (Glassfish/Wildfly etc...) does.
But when i do something like the following the transaction aspect of it works:
#Transactional
public class ServiceClassImpl implements ServiceInterface {
// code here that involves transactions e.g. calling DAO code
...
}
So, i'm confused. I hope someone will enlighten me.
The answer is: NO.
Tomcat 6.x (7&8) don't provide JTA out-of-the-box because they don't have a transaction manager which is required as a separate component to monitor multiple resources (e.g. datasources).
The mentioned answer How to use JTA support in Tomcat 6 for Hibernate? already gives a list of additional JTA transaction managers that can be used alongside Tomcat.
Spring supports declarative transaction management via a platform transaction manager (TM) and provides some implementations (e.g. datasources) that make #Transactional work on a single resource without the additional TM.
Understanding the Spring Framework transaction abstraction provides more details and Spring Boot can be easily configured to run Atomikos or Bitronix Transaction managers on the embedded Tomcat.
JTA provides you with distributed transactions support, but if JTA is not available like in Tomcat, you still can use local JDBC transactions.
YES :-)
JTA can be used in Tomcat, for instance via https://www.atomikos.com
The trick is to use a componentized JTA implementation.
Cheers

JPA Container persistence unit Vs JPA Non-container persistence unit

I am new to JPA and in many resources of JPA I encounter these two term(Container persistence unit and Non-container persistence unit) but I can not understand differences.Can anybody help me understand these phrases? what is the container ?is it something like
Tomcat?
'Container' in this context means an EJB container, which is usually provided by a Java EE compliant application server.
Since Tomcat is not Java EE compliant, it is not a 'container' in the sense of the JPA spec. Tom EE however provides such a container as do other Java EE servers. Oracle provides a complete list of compliant servers.
The main difference is that in a container you can use JTA resources like transactions and a transaction-scoped EntityManager. Without a container you have to use ÈntityTransaction and an EXTENDED EntityManager.
In the attribute transaction-type can take one of the 2 values "RESOURCE_LOCAL" or "JTA".
JTA - Works on an application server environment. Transaction managed by the application server.
RESOURCE_LOCAL - You managed the transaction with your code. You can use this to work standalone during the development / testing phase.

How can I use the same transaction in Spring and an EJB?

I have no development experience with Spring, since we're a Java EE shop. However, we are looking into a solution for our needs that runs on Spring and should integrate our existing Java EE solution.
After reading the Spring 3.0.5 documentation, I'm still uncertain how a transaction can be propagated from Spring to an EJB.
For instance, a Spring bean would create a transaction, save some stuff into one database and then hand over the transaction to one of our stateless session beans, which (using JPA) saves some other stuff into another database. And all of this must run under the same transaction, which is committed when the control is returned to the Spring bean.
Also, I'm not clear about the deployment: Would Spring run as a webapp in the EAR that contains the session beans?
if you configure Spring to use a JTATransactionManager and then call your SessionBean from within Spring the bean should actually pick up the Spring-created transaction. Have look here: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#transaction-application-server-integration and the described <tx:jta-transaction-manager/>
I actually only did it the other way around (joining EJB Transaction in spring) so I'm not completely sure, but you can easily test it by setting
#TransactionAttribute(TransactionAttributeType.MANDATORY)
on your SessionBean and call it from spring... if there's not existing Transaction you will end up with a TransactionRequiredException

JBoss RMI Transaction

How can i can achieve remote transaction while using Remote EJB (over RMI/IIOP or RMI/JRMP).
Is that JBoss 4.0 support this kind of transaction or should i use jotm or atomikos?
Thanks in advance
JBoss 4 is a certified J2EE 1.4 application server and thus supports client-controlled transaction which are part of the J2EE specification. In other words, JBoss provides a Transaction Manager, there is no need for a standalone transaction manager like JOTM, Atomikos, etc.
For the record, JBoss default transaction manager is based on Arjuna TS since JBoss 4.2 which is a rock solid technology.
See the chapter 4.2.3. UserTransaction Support for more details.
From jboss.org. This example is from v. 3.2 but I know it works through v. 4.0.3
There is no way to handle transactions from the client without using a TM on the client. The way to design apps is to call services on the server that handle that for you. All J2EE containers that include JTS/JTA (Ones that are more than just web app servers) will support single a two-phase transaction processing.
The client piece should be in charge of just doing presentation and possibly doing calculations as well as they pertain to displaying the data.
if you must use this strategy, go ahead use a Transaction Coordinator (TM) on the client like JOTM or Atomikos or even possibly JBOSS's JBoss transactions.

Categories

Resources