50
Code simple, but not simpler Discuss ways to develop enterprise applications based on “POJOs in Action” by Chris Richardson

Чурюканов Вячеслав, “Code simple, but not simpler”

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Чурюканов Вячеслав, “Code simple, but not simpler”

Code simple, but not simpler

Discuss ways to develop enterprise applications based on “POJOs in

Action” by Chris Richardson

Page 2: Чурюканов Вячеслав, “Code simple, but not simpler”

Who am I?

Viachaslau Churukanau

Senior Software Engineer, Java world

Edmunds.com, room 404

Page 3: Чурюканов Вячеслав, “Code simple, but not simpler”

INTRODUCTION

Page 4: Чурюканов Вячеслав, “Code simple, but not simpler”

You have to design enterprise applications at some moment

But how to develop this skill if:• There are no appropriate trainings• The Internet usually provides little,

contradictory and out of date knowledge• People tell you “lie”• Etc.?

Page 5: Чурюканов Вячеслав, “Code simple, but not simpler”

Correct Solution

Read Books and Try

Page 6: Чурюканов Вячеслав, “Code simple, but not simpler”
Page 7: Чурюканов Вячеслав, “Code simple, but not simpler”

POJOs in Action (2006)

• shows how to address many common and complex design issues in back-end logic development of enterprise applications with fantastic real-world examples

• combines best practices and design wisdom to integrate domain-driven design and test-driven development for object-oriented applications

• carefully explains the techniques and technologies used at various levels, including Spring, JDO, Hibernate, EJB 3, and iBatis.

Page 8: Чурюканов Вячеслав, “Code simple, but not simpler”

Key Messages

• Regardless of how the frameworks evolve, there are some key concepts that will not change

• Every technology has both benefits and drawbacks

• Make everything as simple as possible, but not simpler (Albert Einstein)

Page 9: Чурюканов Вячеслав, “Code simple, but not simpler”

Design Decisions

Page 10: Чурюканов Вячеслав, “Code simple, but not simpler”

ORGANIZING THE BUSINESS LOGIC

Page 11: Чурюканов Вячеслав, “Code simple, but not simpler”

Domain Model (object-oriented)

Page 12: Чурюканов Вячеслав, “Code simple, but not simpler”

Money Transfer Example

Page 13: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and Drawbacks

+ Improved maintainability and extensibility+ Persistence framework enables you to easily map the domain model to the database- You should have good OOD skills

Page 14: Чурюканов Вячеслав, “Code simple, but not simpler”

Transaction Script (Procedural)

Page 15: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and Drawbacks

+ Easy to Use (no OOD skill required)+ Can use full range of SQL features- Code can be difficult to understand and maintain (complex business logic, explicit load and save all required data)- Cost of maintaining handwritten SQL (e.g. add a column – change multiple queries)- Lack of portability of SQL

Page 16: Чурюканов Вячеслав, “Code simple, but not simpler”

When to Use

• The application must use SQL directly or you cannot use a persistence framework

• The business logic is very simple• The development team lacks the necessary

OO design skills

Page 17: Чурюканов Вячеслав, “Code simple, but not simpler”

ENCAPSULATING THE BUSINESS LOGIC

Page 18: Чурюканов Вячеслав, “Code simple, but not simpler”

EJB session / POJO facade

Page 19: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and Drawbacks

+ Business Logic is encapsulated and is free of transaction management (including distributed transactions), security, remote access, detaching objects, etc.- You must write extra code- It is quite easy for the detachment code and the presentation tier to get out of sync and for the POJO façade to only return some of the objects required by the presentation tier

Page 20: Чурюканов Вячеслав, “Code simple, but not simpler”

Exposed Domain Model (Open Session in View)

Page 21: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and Drawbacks

+ Faster development (the business tier does not contain façades or error-prone detachment logic)- Less encapsulation (it is quite easy for business logic to creep into the presentation tier; the presentation tier has access to the domain objects; business tier changes affect presentation tier)- No support for remote access- Tricky to configure interactions between transactions, persistence framework, and the servlet API

Page 22: Чурюканов Вячеслав, “Code simple, but not simpler”

ACCESSING THE DATABASE

Page 23: Чурюканов Вячеслав, “Code simple, but not simpler”

What’s wrong with using JDBC directly

• Developing and maintaining SQL is difficult and time consuming

• There is a lack of portability with SQL• Writing JDBC code is time consuming and

error-prone

Page 24: Чурюканов Вячеслав, “Code simple, but not simpler”

What if I need SQL?

• You must use the full power of SQL, including vendor-specific features, in order to get good performance• Your DBA might demand complete control over the SQL statements executed by your application• The corporate investment in its relational databases is so massive that the applications working with the databases can appear relatively unimportant

Page 25: Чурюканов Вячеслав, “Code simple, but not simpler”

iBatis

• Insulates the application from connections and prepared statements, iBATIS maps JavaBeans to SQL statements using XML descriptor files or annotations

• Supports for database-generated primary keys, automatic loading of related objects, caching, and lazy loading

Page 26: Чурюканов Вячеслав, “Code simple, but not simpler”

Why you don’t want to persist objects yourself

1. Lazy Loading (loading all of the objects that might be accessed might be extremely inefficient)

2. Writing back to the database only those objects that have been modified (it would be extremely inefficient to save all objects regardless of whether they have changed)

3. The database access code must preserve object identity by ensuring that there is a single in-memory instance of a persistent object when processing a request

Page 27: Чурюканов Вячеслав, “Code simple, but not simpler”

Solution

Use ORM Framework

Page 28: Чурюканов Вячеслав, “Code simple, but not simpler”

Кey features of an ORM framework

• Declarative mapping between the object model and database schema

• An API for creating, reading, updating, and deleting objects

• A query language• Support for transactions• Lazy and eager loading• Caching• Detached objects

Page 29: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and Drawbacks

+ Improved productivity+ Improved performance+ Improved portability+- Sometimes you must use SQL directly

(performance, etc.)- Challenging when you’re working with a legacy

schema (e.g. denormalized)- You might design a domain model that cannot be

mapped to the desired database schema

Page 30: Чурюканов Вячеслав, “Code simple, but not simpler”

HANDLING CONCURRENCYIN DATABASE TRANSACTIONS

Page 31: Чурюканов Вячеслав, “Code simple, but not simpler”

Read Phenomena

Page 32: Чурюканов Вячеслав, “Code simple, but not simpler”

Isolated Database Transactions

• The database ensures that the outcome of executing multiple serializable transactions is the same as executing them serially.

• Serializable transactions prevent such problems as lost updates and inconsistent reads

Page 33: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and Drawbacks

+ They are simple to use+ They prevent many concurrency problems,

including lost updates and inconsistent reads- They produce the high overhead, which can

reduce performance and scalability- Fully isolated transactions can fail more

frequently than less isolated transactions because of deadlocks and other concurrency-related issues

Page 34: Чурюканов Вячеслав, “Code simple, but not simpler”

When to Use

• Read consistency is essential• The overhead of fully isolated transactions is

acceptable

Page 35: Чурюканов Вячеслав, “Code simple, but not simpler”

Pessimistic locking

Page 36: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and drawbacks

+ Unlike optimistic locking, pessimistic locking does not require any schema changes

+ It prevents a transaction from overwriting another transaction’s changes

+ It reduces the probability of deadlocks in databases that implement fully isolated transactions by locking rows when they read

- All potentially conflicting transactions have to use SELECT FOR UPDATE in order for pessimistic locking to work, which is potentially error-prone

- Some databases have limitations on how SELECT FOR UPDATE can be used

Page 37: Чурюканов Вячеслав, “Code simple, but not simpler”

When to Use

• The database schema does not support optimistic locking because, for example, the tables do not have a version or timestamp column or contain values such as floats or blobs that cannot be compared

• The application requires some degree of read consistency

• You don’t want to incur the overhead of serializable transactions

Page 38: Чурюканов Вячеслав, “Code simple, but not simpler”

Optimistic locking

Page 39: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and Drawbacks

+ It is easy to implement and it is supported by many persistence frameworks

+ Unlike pessimistic locking, does not prevent an application from using certain SQL SELECT statement features

- All potentially conflicting transactions must use optimistic locking or errors will occur

- No guarantee that a transaction will be able to update the rows that it read

- Does not prevent inconsistent reads

Page 40: Чурюканов Вячеслав, “Code simple, but not simpler”

When to Use

General recommendation is that an application should use optimistic locking unless:• The database schema does not support

optimistic locking• The application must be guaranteed to be

able to update the rows that it read• The application requires consistent reads

Page 41: Чурюканов Вячеслав, “Code simple, but not simpler”

HANDLING CONCURRENCYIN LONG TRANSACTIONS

Page 42: Чурюканов Вячеслав, “Code simple, but not simpler”

Edit-style Use Case

Page 43: Чурюканов Вячеслав, “Code simple, but not simpler”

What’s wrong with using a single database transaction

• The database transaction would be long-running because it encompasses multiple web requests and user think time

• The transaction might last until the web session timed out if the user could simply walk away from the browser without completing the use case

• It reduce scalability and concurrency

Page 44: Чурюканов Вячеслав, “Code simple, but not simpler”

Pessimistic Offline Lock

Page 45: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and Drawbacks

+ Ensures that a user can save changes- Impacts the application globally (don’t forget

to use it in a new code, etc.)- Requires a mechanism to forcibly release locks

Page 46: Чурюканов Вячеслав, “Code simple, but not simpler”

When to Use

• The probability of conflicts is high• The consequences of conflicts are severe• Users typically do not abandon their sessions

or it’s feasible to implement a lock cleanup mechanism

Page 47: Чурюканов Вячеслав, “Code simple, but not simpler”

Optimistic Offline Lock

Page 48: Чурюканов Вячеслав, “Code simple, but not simpler”

Benefits and Drawbacks

+ It is relatively easy to implement+ Unlike the Pessimistic Offline Lock pattern, there are no locks

to clean up if the user abandons the session, which is not uncommon in a web application.

- Because the Optimistic Offline Lock pattern only detects changes when the user tries to save their changes, it only works well when starting over is not a burden on the user

- All transactions that update shared data must increment the version number whenever they update a row, including those that do not use the Optimistic Offline Lock pattern

Page 49: Чурюканов Вячеслав, “Code simple, but not simpler”

When to Use

• The probability of conflicts is low and the consequences of redoing the changes are minimal

• Users regularly abandon sessions and you don’t want to implement a lock cleanup mechanism

Page 50: Чурюканов Вячеслав, “Code simple, but not simpler”

Summary