Java/EJB3/Transaction

Материал из Java эксперт
Перейти к: навигация, поиск

Container Managed Transaction

File: EmployeeService.java

import java.util.Collection;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  public EmployeeService() {
  }
  public Employee createEmployee(int id, String name, long salary, byte[] pic) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    emp.setPicture(pic);
    
    em.persist(emp);
    
    
    emp = findEmployee(id);
    System.out.println(emp);
    
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
    Employee emp = new Employee(1);
    emp.setName("name");
    emp.setSalary(100);
    emp.setPicture("pic".getBytes());
    
    em.persist(emp);
    
    
    emp = findEmployee(1);
    System.out.println(emp);
  }
}
    

File: EmployeeServiceLocal.java

import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
}

File: EmployeeServiceRemote.java


import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
  
  public void doAction();
}

File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  @Basic(fetch=LAZY)
  @Lob @Column(name="PIC")
  private byte[] picture;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }
  public byte[] getPicture() {
    return picture;
  }
  public void setPicture(byte[] picture) {
    this.picture = picture;
  }
  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.naming.InitialContext;
public class Main {
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeService/remote");
    service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
    service.doAction();
  }
}





Container Managed Transaction: Required

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName = "EmployeeService")
  private EntityManager em;

  public EmployeeBean() {
  }
  @TransactionAttribute(TransactionAttributeType.REQUIRED)
  public void doAction() {
    Employee emp = new Employee();
    emp.setFirstName("first");
    em.persist(emp);
  }
  public <T> T mergeEntity(T entity) {
    return em.merge(entity);
  }
  public <T> T persistEntity(T entity) {
    em.persist(entity);
    return entity;
  }
  public <T> T refreshEntity(T entity) {
    em.refresh(entity);
    return entity;
  }
  public void removeEntity(Object entity) {
    em.remove(em.merge(entity));
  }
}

File: EmployeeServiceLocal.java

import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
  public void doAction();
  <T> T mergeEntity(T entity);
  <T> T persistEntity(T entity);
  <T> T refreshEntity(T entity);
  void removeEntity(Object entity);
}

File: EmployeeServiceRemote.java
import javax.ejb.Remote;

@Remote
public interface EmployeeServiceRemote {
  public void doAction();
  <T> T mergeEntity(T entity);
  <T> T persistEntity(T entity);
  <T> T refreshEntity(T entity);
  void removeEntity(Object entity);

}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.naming.InitialContext;

public class Main {
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new
    // InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");

    service.doAction();
  }
}





Container Managed Transaction: TransactionAttributeType.SUPPORTS

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
@Stateful
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {

  public EmployeeBean() {
  }
  @TransactionAttribute(TransactionAttributeType.SUPPORTS)
  public void doAction() {
     System.out.println("Processing...");
  }
}

File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void doAction();
}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
  
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}





Inject Transaction As Resource

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
import javax.annotation.Resource;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.transaction.UserTransaction;
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @Resource
  UserTransaction tx;
  public EmployeeBean() {
  }
  public void doAction() {
    try {
      try {
        tx.begin();
        // do the work...
        System.out.println("Processing...");
      } finally {
        tx.rumit();
      }
    } catch (Exception e) {
      // handle all the tx.begin()/commit() exceptions
      throw new EJBException(e);
    }
  }
}

File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void doAction();
}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
  
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}





Mark Stateful Session Bean As Transaction Attribute Type: REQUIRES_NEW

File: EmployeeBean.java
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
@Stateful
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName = "EmployeeService", type = PersistenceContextType.EXTENDED)
  EntityManager em;
  public EmployeeBean() {
  }
  public void doAction() {
    System.out.println("Processing...");
  }
}

File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void doAction();
}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
  
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099





Transaction Attribute Type: REQUIRES_NEW

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {

  public EmployeeBean() {
  }
  @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  public void doAction() {
     System.out.println("Processing...");
  }
}

File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void doAction();
}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
  
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}





Transaction Attribute Type: SUPPORTS

File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeService.java

import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
@Stateful
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService", type=PersistenceContextType.EXTENDED)
  private EntityManager entityManager;
  public EmployeeService() {
  }

  public void doAction() throws Exception {
    Employee emp = new Employee();
    emp.setId(1);
    entityManager.merge(emp);
  }
}

File: EmployeeServiceLocal.java

import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction()  throws Exception;

}

File: EmployeeServiceRemote.java


import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public void doAction()  throws Exception;  
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import java.util.Date;
import javax.naming.InitialContext;

public class Main {
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeService/remote");
    
    

    
    service.doAction();
  }
}





Transaction Management Type: BEAN

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
import javax.annotation.Resource;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.transaction.UserTransaction;
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @Resource
  UserTransaction tx;
  public EmployeeBean() {
  }
  public void doAction() {
    try {
      try {
        tx.begin();
        // do the work...
        System.out.println("Processing...");
      } finally {
        tx.rumit();
      }
    } catch (Exception e) {
      // handle all the tx.begin()/commit() exceptions
      throw new EJBException(e);
    }
  }
}

File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void doAction();
}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
  
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}





Use User Transaction In Client Side

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java

import javax.annotation.Resource;
import javax.ejb.Stateful;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.transaction.UserTransaction;
@Stateful
@TransactionManagement(TransactionManagementType.BEAN)
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName = "EmployeeService", type = PersistenceContextType.EXTENDED)
  private EntityManager em;
  @Resource
  private UserTransaction ut;
  
  public EmployeeBean() {
  }

  public void doAction() {
    Employee emp = new Employee();
    emp.setFirstName("first");
    em.persist(emp);
  }
  public <T> T mergeEntity(T entity) {
    return em.merge(entity);
  }
  public <T> T persistEntity(T entity) {
    em.persist(entity);
    return entity;
  }
  public <T> T refreshEntity(T entity) {
    em.refresh(entity);
    return entity;
  }
  public void removeEntity(Object entity) {
    em.remove(em.merge(entity));
  }
  public void beginTrans()
    throws Exception {
    ut.begin();
  }
  public void commitTrans()
    throws Exception {
    ut.rumit();
  }
  public void rollbackTrans()
    throws Exception {
    ut.rollback();
  }
}

File: EmployeeServiceLocal.java

import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
  public void doAction();
  <T> T mergeEntity(T entity);
  <T> T persistEntity(T entity);
  <T> T refreshEntity(T entity);
  void removeEntity(Object entity);
  void beginTrans() throws Exception;
  void commitTrans() throws Exception;
  void rollbackTrans() throws Exception;
}

File: EmployeeServiceRemote.java
import javax.ejb.Remote;

@Remote
public interface EmployeeServiceRemote {
  public void doAction();
  <T> T mergeEntity(T entity);
  <T> T persistEntity(T entity);
  <T> T refreshEntity(T entity);
  void removeEntity(Object entity);
  void beginTrans() throws Exception;
  void commitTrans() throws Exception;
  void rollbackTrans() throws Exception;
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.naming.InitialContext;

public class Main {
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new
    // InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.beginTrans();
    service.doAction();
    service.rumitTrans();
  }
}