Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

622 views

Published on

  • Be the first to comment

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

  1. 1. Code simple, but not simpler Discuss ways to develop enterprise applications based on “POJOs in Action” by Chris Richardson
  2. 2. Who am I?Viachaslau Churukanau Senior Software Engineer, Java world Edmunds.com, room 404
  3. 3. INTRODUCTION
  4. 4. You have to design enterprise applications at some momentBut 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.?
  5. 5. Correct SolutionRead Books and Try
  6. 6. 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.
  7. 7. 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)
  8. 8. Design Decisions
  9. 9. ORGANIZING THE BUSINESS LOGIC
  10. 10. Domain Model (object-oriented)
  11. 11. Money Transfer Example
  12. 12. Benefits and Drawbacks+ Improved maintainability and extensibility+ Persistence framework enables you to easilymap the domain model to the database- You should have good OOD skills
  13. 13. Transaction Script (Procedural)
  14. 14. Benefits and Drawbacks+ Easy to Use (no OOD skill required)+ Can use full range of SQL features- Code can be difficult to understand andmaintain (complex business logic, explicit loadand save all required data)- Cost of maintaining handwritten SQL (e.g. adda column – change multiple queries)- Lack of portability of SQL
  15. 15. 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
  16. 16. ENCAPSULATING THE BUSINESSLOGIC
  17. 17. EJB session / POJO facade
  18. 18. Benefits and Drawbacks+ Business Logic is encapsulated and is free oftransaction management (including distributedtransactions), security, remote access, detachingobjects, etc.- You must write extra code- It is quite easy for the detachment code andthe presentation tier to get out of sync and forthe POJO façade to only return some of theobjects required by the presentation tier
  19. 19. Exposed Domain Model (Open Session in View)
  20. 20. Benefits and Drawbacks+ Faster development (the business tier does notcontain façades or error-prone detachment logic)- Less encapsulation (it is quite easy for businesslogic to creep into the presentation tier; thepresentation tier has access to the domain objects;business tier changes affect presentation tier)- No support for remote access- Tricky to configure interactions betweentransactions, persistence framework, and theservlet API
  21. 21. ACCESSING THE DATABASE
  22. 22. 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
  23. 23. What if I need SQL?• You must use the full power of SQL, includingvendor-specific features, in order to get goodperformance• Your DBA might demand complete control overthe SQL statements executed by yourapplication• The corporate investment in its relationaldatabases is so massive that the applicationsworking with the databases can appearrelatively unimportant
  24. 24. 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
  25. 25. Why you don’t want to persist objects yourself1. 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
  26. 26. SolutionUse ORM Framework
  27. 27. К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
  28. 28. 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
  29. 29. HANDLING CONCURRENCYIN DATABASE TRANSACTIONS
  30. 30. Read Phenomena
  31. 31. 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
  32. 32. 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
  33. 33. When to Use• Read consistency is essential• The overhead of fully isolated transactions is acceptable
  34. 34. Pessimistic locking
  35. 35. 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
  36. 36. 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
  37. 37. Optimistic locking
  38. 38. 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
  39. 39. When to UseGeneral recommendation is that an applicationshould 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
  40. 40. HANDLING CONCURRENCYIN LONG TRANSACTIONS
  41. 41. Edit-style Use Case
  42. 42. 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
  43. 43. Pessimistic Offline Lock
  44. 44. 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
  45. 45. 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
  46. 46. Optimistic Offline Lock
  47. 47. 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
  48. 48. 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
  49. 49. Summary

×