business layer and transactions

Download Business layer and transactions

If you can't read please download the document

Upload: ondrej-mihalyi

Post on 14-Apr-2017

203 views

Category:

Technology


0 download

TRANSCRIPT

Business layer

Lecturer: Ondrej Mihlyi

ContentsMultilayered 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 transactiontx.commit

} catch (Exception e) {tx.rollback();

}

How simple can transactions be?

@Inject ServiceBean service;// before transactionservice.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 beansExexcuted 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 StatefulReused 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 StatelessNo state, can be pooled, shared by clients

@MessageDriven

When Message-driven?

Asynchronous processingDistribute 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 applicationApplication 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 interfaceMarked 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