Bean managed Transactions
With this example we are going to demonstrate how to handle bean managed transactions using EJBs. Enterprise beans are Java EE components that implement Enterprise JavaBeans (EJB) technology. Enterprise beans run in the EJB container, a runtime environment within the Application Server (see Container Types). Although transparent to the application developer, the EJB container provides system-level services such as transactions and security to its enterprise beans. These services enable you to quickly build and deploy enterprise beans, which form the core of transactional Java EE applications.
Here, we have created an EJB implementation, EmployeeService
where we create and handle transactions, as described below:
The Data Transfer (Domain) object
Class Employee
is an entity, that is a lightweight persistence domain object. Typically an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The persistent state of an entity is represented either through persistent fields or persistent properties. These fields or properties use object/relational mapping annotations to map the entities and entity relationships to the relational data in the underlying data store. The class is annotated with the javax.persistence.Entity
annotation, it has a public or protected, no-argument constructor and it implements the Serializable interface.
package com.javacodegeeks.snippets.enterprise; import java.io.Serializable; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Employee implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String name; private String surname; private String title; private Double salary; private Date created; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", surname=" + surname + ", title=" + title + ", salary="+salary+ ", created=" + created+"]"; } }
The EmployeeService EJB implementation class
The EmployeeService
EJB implementation class is an EJB implementation class that is a stateless session bean and has a reference of the CalculatorService
EJB. It is annotated with the javax.ejb.TransactionManagement
annotation of type javax.ejb.TransactionManagementType.BEAN
, to specify that it has bean managed transactions. It uses the javax.persistence.EntityManager
interface, that creates and removes persistent entity instances, finds entities by the entity’s primary key, and allows queries to be run on entities. The EntityManager
is annotated with the javax.persistence.PersistenceContext
annotation to express a dependency on the container-managed EntityManager
and its associated persistence context, using the name of the persistence unit as defined in the persistence.xml
file. The UserTransaction
interface is used to define the methods that allow an application to explicitly manage transaction boundaries. The EmployeeService
also has a local and a remote interface.
package com.javacodegeeks.snippets.enterprise; import java.util.Collection; import java.util.Date; import javax.annotation.Resource; import javax.ejb.EJBException; import javax.ejb.Stateless; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.transaction.UserTransaction; @Stateless @TransactionManagement(TransactionManagementType.BEAN) public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote { @PersistenceContext(unitName = "TutorialPU") EntityManager entityManager; @Resource UserTransaction tx; public EmployeeService() { } public Employee createEmployee(String name, String surname, String title, double salary) { Employee employee = new Employee(); employee.setName(name); employee.setSurname(surname); employee.setTitle(title); employee.setSalary(salary); employee.setCreated(new Date()); try { try { tx.begin(); entityManager.persist(employee); } finally { tx.commit(); } } catch (Exception e) { // handle all the tx.begin()/commit() exceptions throw new EJBException(e); } return employee; } public void removeEmployee(long id) { Employee employee = findEmployee(id); if (employee != null) { try { try { tx.begin(); entityManager.remove(employee); } finally { tx.commit(); } } catch (Exception e) { // handle all the tx.begin()/commit() exceptions throw new EJBException(e); } } } public Employee promoteEmployee(long id, String newTitle, double newSalary) { Employee employee = entityManager.find(Employee.class, id); if (employee != null) { employee.setTitle(newTitle); employee.setSalary(newSalary); try { try { tx.begin(); entityManager.merge(employee); } finally { tx.commit(); } } catch (Exception e) { // handle all the tx.begin()/commit() exceptions throw new EJBException(e); } } return employee; } public Employee findEmployee(long id) { return entityManager.find(Employee.class, id); } public Collection<Employee> findAllEmployees() { Query query = entityManager.createQuery("SELECT e FROM Employee e"); return (Collection<Employee>) query.getResultList(); } }
The EJB local interface (suitable for in VM communication)
package com.javacodegeeks.snippets.enterprise; import java.util.Collection; import javax.ejb.Local; @Local public interface EmployeeServiceLocal { public Employee createEmployee(String name, String surname, String title, double salary); public void removeEmployee(long id); public Employee promoteEmployee(long id, String newTitle, double newSalary); public Employee findEmployee(long id); public Collection<Employee> findAllEmployees(); }
The EJB remote interface (suitable for intra VM communication)
package com.javacodegeeks.snippets.enterprise; import java.util.Collection; import javax.ejb.Remote; @Remote public interface EmployeeServiceRemote { public Employee createEmployee(String name, String surname, String title, double salary); public void removeEmployee(long id); public Employee promoteEmployee(long id, String newTitle, double newSalary); public Employee findEmployee(long id); public Collection<Employee> findAllEmployees(); }
The persistence.xml file driving the JPA framework
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="TutorialPU" > <jta-data-source>java:/DefaultDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> <!-- <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> <property name="hibernate.connection.username" value="sa"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.url" value="jdbc:hsqldb:data/tutorial"/> </properties> --> </persistence-unit> </persistence>
The application.xml file describing the modules in the .ear archive
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" version="1.4"> <display-name>ExampleEJB3</display-name> <module> <java>exampleEJB3-persistence.jar</java> </module> <module> <ejb>exampleEJB3.jar</ejb> </module> </application>
The structure of the .ear archive
exampleEJB3.ear | |_exampleEJB3-persistence.jar | |_com | | |_javacodegeeks | | |_snippets | | |_enterprise | | |_Employee.class | |_META-INF | |_persistence.xml | |_exampleEJB3.jar | |_com | | |_javacodegeeks | | |_snippets | | |_enterprise | | |_EmployeeService.class | | |_EmployeeServiceLocal.class | | |_EmployeeServiceRemote.class | |_META-INF | |_META-INF |_application.xml
A simple client example
In EmployeeServiceClient
we connect to JBoss naming service running on local host and on default port 1099. We use the Context to set the configuration for the JBoss server, such as Context.INITIAL_CONTEXT_FACTORY
, Context.PROVIDER_URL
and Context.URL_PKG_PREFIXES
. We get the EmployeeService
EJB using the lookup(Name name)
method of Context to invoke its methods.
package com.javacodegeeks.snippets.enterprise; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; public class EmployeeServiceClient { public static void main(String[] a) throws Exception { /* * Connecting to JBoss naming service running on local host and on * default port 1099 the environment that should be created is like the * one shown below : */ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); env.put(Context.PROVIDER_URL, "jnp://localhost:1099"); env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); Context ctx = new InitialContext(env); // We get a reference of the remote EJB interface to invoke its business methods EmployeeServiceRemote employeeService = (EmployeeServiceRemote) ctx.lookup("exampleEJB3/EmployeeService/remote"); Employee employee = employeeService.createEmployee("Byron", "Kiourtzoglou", "Master Software Engineer", 2000d); long employeeId = employee.getId(); System.out.println(employeeService.findEmployee(employeeId)); employeeService.promoteEmployee(employeeId, "Principal Software Engineer", 3000d); System.out.println(employeeService.findEmployee(employeeId)); } }
Output:
Employee [id=1, name=Byron, surname=Kiourtzoglou, title=Master Software Engineer, salary=2000.0, created=2011-12-03 17:31:30.203]
Employee [id=1, name=Byron, surname=Kiourtzoglou, title=Principal Software Engineer, salary=3000.0, created=2011-12-03 17:31:30.203]
This was an example of how to handle bean managed transactions using EJBs.
Cool simple example…. thanks..