How do JTA Transaction Managers deploy at runtime? - java

Trying to wrap my head around JTA and have arbitrarily chosen Bitronix as the impl because the documentation was easier to get to (as opposed to Atmikos which makes you sign-up and register in order to get at the src/docs/jars/etc.).
If I want to use Bitronix to be my JTA implementation (using Tomcat & GlassFish), then what is its basic architecture (which may just be the basic architecture of JTA itself)? Is a transaction manager an actual server/runtime that I connect to (like a JMS broker)? Or is this just an API that I can configure and hit whenever I need a transaction?
My understanding of JTA is that there is:
Your code
A resource manager - adaptor for some ACID-compliant persistence (like a datastore or message broker)
A transaction manager - manages transaction API calls between your code and the resource
manager
Is Bitronix just the transaction manager and if so is it a separate application, a separate JAR/WAR that has to be deployed alongside yours, or does it run "embedded" inside your app? Thanks in advance!

It runs embedded inside Tomcat, and is accessible through JNDI, like all other JTA transaction managers. The whole process of embedding Bitronix with Tomcat is described here.
Note that there is no reason to use Bitronix with Glassfish, since Glassfish is a full-stack Java EE app server and thus already has a JTA transaction manager.

Related

Do we need separate XA transaction manager configuration in an application using JMS and JDBC?

I have a spring boot application using generic maven components - one for IBM MQ configuration and the other for database configuration. Both these maven components were developed by us.
We need to setup XA transaction in this application to ensure that input MQ messages coming into the application via IBM MQ and then persists the message to Database.
We are planning to use Atomikos transaction manager.
My query is - Do we need separate transaction manager configured i.e. one each for JDBC and MQ (JMS) here?
You can use the standard machinery to deal with distributed (or local) transactions but you need configure some transaction manager.
If you use Java enterprise services, the use of transaction for JMS are described (e.g.) here, here and here (typically the #Transactional annotation).
If not, for spring boot you can configure, without special coding (i.e. using again #Transactional), the atomikos transaction manager as described here. Basically:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>

What is the default Session Storage for Spring Boot?

I have read the Spring Boot documentation and I got knowledge about Spring Session from this document.
But I see that HttpSession class works without adding any dependencies in my code.
JDBC
Redis
Hazelcast
MongoDB
So...
I'm guessing that Spring Boot provides Session Storage by default. Does anyone know about this? I looked at spring docs but couldn't find it.
How to you run your app? Do you use Tomcat or Jetty embedded server or deploy it in those servers? They all have their own session storage implementation. Tomcat uses file-bases session storage by default.
The session storage that you mentioned by default is provided by the Servlet container.It is just an internal java.util.Map.
Spring Session is a Spring sub-project. It is optional and its purpose is to allow you swap the session stroage mechanism provided by the container with the one provided by Spring Session which can be RDBMS, Redis ,Hazelcast Cluster or MongoDB .HttpSession is then stored in the container agnostic way such that you can have session persistence /session clustering more easier. You no longer need to consult the Servlet container documentation for how to configure a cluster etc.
If you want to use Spring session, you still have to include the dependencies from the spring session project.

Calling an EJB3 library from an EJB2 library

I have an existing application that is written in EJB2.1. This is deployed on jboss-5.1.0.GA as an EAR.
I now have a new requirement to implement a process which is supposed to be used by the EJB2.1 application. Both will be in their own ear files but will both be running on the same jboss-5.1.0 instance.
My questions are:
Is it possible to call the services of the EJB3 ear from the EJB2.1 ear?
If the answer to the above is yes, is it possible to manage the transactions? i.e. only have the EJB2.1 application commit any transactions.
Migrating the existing 2.1 application to v3 is out of the question. Is creating the new process as v3 worth the effort or am i likely to come across problems with the integration between the two?
I have been looking around for a simple hello world type example that demonstrate the above but have not been able to find any. Anyone know of a good example?
The EJB3 services will be called from session beans in the EJB2 application. Is this the wrong way to do it? (i.e. the session bean is not a client).
The EJB3 specification allows a smooth transition,defining methods for EJB2/EJB3 interoperability, it's sometimes bulky and you will need artifacts not necessary in a 'normal' EJB3 app (#RemoteHome)
Is it possible to call the services of the EJB3 ear from the EJB2.1 ear?
Yes, it is possible, see: Referencing EJB3 beans in EJB2.1 and
Introduction to using EJB2.1 client adaptors
and the sample project "ejb21_client_adaptors"
If the answer to the above is yes, is it possible to manage the transactions? i.e. only have the EJB2.1 application commit any transactions.
Yes, just remember to annotate your EJB3 method with #TransactionAttribute(TransactionAttributeType.MANDATORY)
The EJB3 services will be called from session beans in the EJB2 application. Is this the wrong way to do it? (i.e. the session bean is not a client).
I see no problem here, session beans can call other beans' services.

EJB, spring & hibernate

I've been doing Java standard edition development for awhile, and the frameworks and solutions have been fairly obvious to me (Swing for GUI, etc). I've recently begun the task of building a server for my own needs and to host a web page (things the server will be doing in addition to hosting a web page would include personal SVN hosting and integrating more web functionality into existing and future applications). For coding for only a single computer (that is, distributed computing, etc. is not really a concern)- I'm not entirely certain between Spring, Hibernate and EJB and am not very knowledgeable as to the capabilities of each. Information would be appreciated. I know Spring is an alternative to EJB, and Hibernate is an object-relational mapping library, so does EJB combine the two?
EJB3 in summary consists of 3 bean types; Message Driven Beans, Stateless/Stateful Session Beans and Entity Beans or JPA (Java Persistance Architecture). Hibernate can be used as a JPA provider, but it doesn't have to be used that way.
Spring has functionality that is roughly similar to Message Driven Beans and Stateless/Stateful session beans but it does not have an equivalent to JPA. Instead it allows you to utilize JPA or hibernate directly but if you do combine Spring with JPA you'll need an EJB container.
Another difference is that EJB is provided by 'containers' whereas spring is a framework that can be deployed into a java se application or a java servlet container web application like jetty or tomcat. Its an important distinction to make. Tomcat is not an EJB container, only a servlet container, hence if you were to only use basic tomcat, you couldn't use EJBs but you could use spring by including the required spring jar files in your web application (WEB-INF/lib). In that case you wouldn't use JPA either but you could use hibernate.
Some java servers that ARE EJB containers include weblogic, websphere, jboss, geronimo, and glassfish. Tomcat can become an EJB container if you combine it with OpenEJB.
In an application i'm developing at the moment, I'm using tomcat + openejb so I have an EJB container so I can use JPA with OpenJPA as the persistance provider. I'm using Spring MVC to do the web application development and will most likely use spring security as well down the track. I probably will also use Stateless Session EJBs as well to encapsulate business functionality but i could just as easily use Spring service/dao beans instead of statless session ejbs as well.
Its pretty confusing sometimes to work out what parts of which framework or container you should use and really comes down to either preference or using functionality in one that doesn't exist in the other or ease of use. Another consideration is memory utilization. Some of the ejb containers consume large amounts of memory just starting up with no applications running. This is one reason I ended up using tomcat + openejb. Hope this helps.

Java EE without Application Server

Since EJB 3 we have embeddable EJB containers, JPA implementations can be used without an application server, there is Weld for contexts and dependency injection and so on. Since on many systems is only Tomcat available, I wonder, if Java EE could be used without an application server but with a Servlet container like Tomcat.
What would I have to do to set up an Java environment? What drawbacks do you see?
Note that Tomcat is an Application Server. That said, in October we released Apache TomEE which is Tomcat with the missing JavaEE parts added, then Java EE 6 certified using the official TCK from Oracle.
The stack evolved from what used to be simply called "OpenEJB/Tomcat", which was a useful stack with a bad name :) Commonly overlooked because of the "EJB" part, meanwhile it also delivered Transactions, JMS, WebServices and more to Tomcat. The new name is far better and now it's officially certified like JBoss or GlassFish. We're pretty excited about its future.
If I understand well, you want to use EJB3/JPA within a servlet container.
There are not only stand-alone implementations of JPA, but also embeddable EJB3 container, such as OpenEJB or Glassfish embeddable container. So nothing prevents you from starting such an embeddable container from the Servlet container to use EJB3.
(Note: I don't know all the details about transactions. In a full-blown app. server, you have JTA and a distributed transaction manager. You don't have that in a Servlet container such as Tomcat. JPA works with JTA and plain JDBC, but I don't know exactly how an embeddable container work without JTA. Still, I guess that would work given that such embeddable containers are also meant for unit-testing where I guess there is no JTA distributed transaction manager.)
Another approach would be to use Spring. Spring and EJB3 have indeed become very similar. You can start the Spring DI container within the Servlet container and benefit more or less from the same facilities as EJB3 (declarative transactions, etc.). See this post about Spring vs. EJB3.
All these technologies have become pretty modular, especially with Java EE profiles. You can use Sevlets, EJB3, JMS, JPA, even JTA somehow independently of one other. You can also create an environment where you cherry pick the ones you would like, either with Spring or with Java EE. The question is when does it stop to make sense and rather use an app. server with everything available and easily manageable. I think Servlet/EJB3/JPA is the limit, if more is needed go for an app. server.
You will generally require some kind of container, even if that container doesn't provide Java EE-related services. After all, you do need a long-lived JVM process to host the code that you're executing. Tomcat and Jetty will do the job nicely, and in addition to basic servlet services, provide a few useful extras that will be relevant, like connection pooling.
J2EE without application server was introduced years ago by me (Guy Pardon, from Atomikos), with this seminal article: http://www.onjava.com/pub/a/onjava/2006/02/08/j2ee-without-application-server.html - focused on JMS and JDBC at the time.
In general it's easy to setup thanks to Spring and Hibernate. Actually, I got inspired to do this after doing a Java EE project and being confronted with the XML hell associated with app servers and EJBs. Without application server things suddenly became a lot simpler and more testable.
If you need a Tomcat installation then can be a bit more of a hassle to configure, but recently Atomikos has introduced out-of-the-box Tomcat integration as part of its commercial offering at http://www.atomikos.com.
HTH

Categories