Business layer
Lecturer: Ondrej Mihályi
Contents
●Multilayered application architecture
●Enterprise Java Beans
●Transaction creation and handling
●Java EE architectural patterns
Layers of Java EE application
Traditional transactions
tx.begin();
try {
// … executing in transaction
tx.commit
} catch (Exception e) {
tx.rollback();
}
How simple can transactions be?
@Inject ServiceBean service;
// before transaction
service.execute(); // running in transaction
// after transaction
What are EJBs?
● Represent service layer
● Components encapsulating business logic
● Automatic handling of transactions
● Accessible remotely outside of application
● Security authorization
Types of EJBs
● Session beans:
– Stateless, Stateful, Singleton
– Accessed directly via a method call
● Message-driven beans
– Exexcuted when message is delivered
Stateful Session Bean
● Stateful instance unique to the client
● Holds state between method calls
● Only one client can access at a time
● Must support serialization (swapping)
● @Stateful
When Stateful?
● The bean's state represents the interaction
between the bean and a specific client.
● The bean needs to hold information about
the client across method invocations.
● Behind the scenes, the bean manages the
work flow of several enterprise beans.
Stateless Session Bean
● State is not held between method calls
● Can be reused by multiple clients
● More efficient than Stateful
– Reused using a pool of beans
● Can implement a web service
● @Stateless
When Stateless?
● The bean does not have state.
● The bean methods are independent.
● The bean implements a web service.
● To improve performance, whenever
Stateful is not necessary.
Singleton Session Bean
● Similar to stateless, but:
– Single instance is reused by all clients
– State is held between method calls
● Can be created on application startup
● @Singleton (javax.ejb.Singleton)
When Singleton?
● When some data is shared by multiple
clients
● To perform an action at application startup
Message-driven Bean
● Single method onMessage(Message msg)
● Called when message received
● Otherwise resembles Stateless
– No state, can be pooled, shared by clients
● @MessageDriven
When Message-driven?
● Asynchronous processing
– Distribute processing on multiple nodes
– Do not block other requests
– Queue execution after pending requests
Transaction management
● @TransactionManagement
● By container (default setting)
– Application server creates and closes transactions
– TransactionManagementType.CONTAINER
● By application
– Application calls methods on transaction object
– TransactionManagementType.BEAN
Container managed Tx
● @TransactionAttribute
● REQUIRED, REQUIRES_NEW,
NOT_SUPPORTED, …
● Configuration on class level, or for each
method
Application managed Tx
● @Inject UserTransaction tx;
● tx.begin(), tx.commit()
Asynchronous invocation
● Execute a business method in another
thread
● Runs in a new transaction
● Method either void or returns Future
● @Asynchronous
Remote business interface
● EJB can be accessible from outside of application
● Needs to implement an interface
– Marked be @Remote
● Exposed in JNDI registry
● Remote method invocation from outside of application
server
DAO pattern
● Data Access Object
● DAO objects are abstraction over persistence
layer
● Provide object-oriented encapsulation
● Single point of querying, entity creation and
persisting
DTO pattern
● Data Transfer Object
● Simple value objects to transfer data
between layers
● Used to transfer data specific to one layer
into another layer in various contexts and
forms
Service facade
● Groups multiple components
● Provides single access methods to execute
complicated business logic
● When multiple business components must run in
single transaction
● Represented by EJBs, which combine logic of multiple
simpler EJBs

Business layer and transactions

  • 1.
  • 2.
    Contents ●Multilayered application architecture ●EnterpriseJava Beans ●Transaction creation and handling ●Java EE architectural patterns
  • 3.
    Layers of JavaEE application
  • 4.
    Traditional transactions tx.begin(); try { //… executing in transaction tx.commit } catch (Exception e) { tx.rollback(); }
  • 5.
    How simple cantransactions be? @Inject ServiceBean service; // before transaction service.execute(); // running in transaction // after transaction
  • 6.
    What are EJBs? ●Represent service layer ● Components encapsulating business logic ● Automatic handling of transactions ● Accessible remotely outside of application ● Security authorization
  • 7.
    Types of EJBs ●Session beans: – Stateless, Stateful, Singleton – Accessed directly via a method call ● Message-driven beans – Exexcuted when message is delivered
  • 8.
    Stateful Session Bean ●Stateful instance unique to the client ● Holds state between method calls ● Only one client can access at a time ● Must support serialization (swapping) ● @Stateful
  • 9.
    When Stateful? ● Thebean's state represents the interaction between the bean and a specific client. ● The bean needs to hold information about the client across method invocations. ● Behind the scenes, the bean manages the work flow of several enterprise beans.
  • 10.
    Stateless Session Bean ●State is not held between method calls ● Can be reused by multiple clients ● More efficient than Stateful – Reused using a pool of beans ● Can implement a web service ● @Stateless
  • 13.
    When Stateless? ● Thebean does not have state. ● The bean methods are independent. ● The bean implements a web service. ● To improve performance, whenever Stateful is not necessary.
  • 14.
    Singleton Session Bean ●Similar to stateless, but: – Single instance is reused by all clients – State is held between method calls ● Can be created on application startup ● @Singleton (javax.ejb.Singleton)
  • 15.
    When Singleton? ● Whensome data is shared by multiple clients ● To perform an action at application startup
  • 16.
    Message-driven Bean ● Singlemethod onMessage(Message msg) ● Called when message received ● Otherwise resembles Stateless – No state, can be pooled, shared by clients ● @MessageDriven
  • 18.
    When Message-driven? ● Asynchronousprocessing – Distribute processing on multiple nodes – Do not block other requests – Queue execution after pending requests
  • 19.
    Transaction management ● @TransactionManagement ●By container (default setting) – Application server creates and closes transactions – TransactionManagementType.CONTAINER ● By application – Application calls methods on transaction object – TransactionManagementType.BEAN
  • 20.
    Container managed Tx ●@TransactionAttribute ● REQUIRED, REQUIRES_NEW, NOT_SUPPORTED, … ● Configuration on class level, or for each method
  • 21.
    Application managed Tx ●@Inject UserTransaction tx; ● tx.begin(), tx.commit()
  • 22.
    Asynchronous invocation ● Executea business method in another thread ● Runs in a new transaction ● Method either void or returns Future ● @Asynchronous
  • 23.
    Remote business interface ●EJB can be accessible from outside of application ● Needs to implement an interface – Marked be @Remote ● Exposed in JNDI registry ● Remote method invocation from outside of application server
  • 24.
    DAO pattern ● DataAccess Object ● DAO objects are abstraction over persistence layer ● Provide object-oriented encapsulation ● Single point of querying, entity creation and persisting
  • 25.
    DTO pattern ● DataTransfer Object ● Simple value objects to transfer data between layers ● Used to transfer data specific to one layer into another layer in various contexts and forms
  • 26.
    Service facade ● Groupsmultiple components ● Provides single access methods to execute complicated business logic ● When multiple business components must run in single transaction ● Represented by EJBs, which combine logic of multiple simpler EJBs