Odtug2011 adf developers make the database work for you

2,279 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,279
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Process
  • http://technology.amis.nl/blog/4162/dynamic-and-conditional-grouping-in-sql-queries-for-flexible-results-from-single-query-oh-and-a-useful-case-for-the-cube-operator
  • http://technology.amis.nl/blog/4191/adf-11g-treetable-with-sub-totals-how-the-sql-query-can-make-life-easier-for-the-view-developer
  • select ename, sal, deptno, job, rank() over (partition by deptno order by saldesc) sal_rank_in_dept, rank() over (partition by job order by saldesc) sal_rank_in_job, lag(sal,1) over (partition by deptno order by saldesc) diff_with_next_higher_in_dept, listagg(ename, ',') within group (order by ename) over (partition by deptno) colleaguesfrom emporderby deptno, saldesc
  • http://technology.amis.nl/blog/3255/integrating-flashback-in-adf-web-applications-providing-the-historic-perspectivehttp://technology.amis.nl/documents/technology/dvt.pdf
  • Cache – spreekuit: kasjeKastjesBrowser: Client (browser, cookie or Java Script memory; HTML 5 offers persistent, cross session local db like storage)App Server : Edge (WebServer)JVM (and cluster)Cross cluster shared cachedb or memory gridDatabase (not requery at least)
  • Screenshot:Frank sends email to Maggie – with a query on EmployeesAfter some time, a response is sent to this particular email – by the queue listener using the JSP to send(list of employee data, corresponding with “query”)
  • Odtug2011 adf developers make the database work for you

    1. 1. ADF Developers – make the database work for you<br />ODTUG Kaleidoscope 2011 – Long Beach, California<br />Luc Bors, AMIS, The Netherlands<br />
    2. 2. Desktop<br />Browser-Based<br />Metadata Services (MDS)<br />Oracle ADF – Role of the Database<br />JSF<br />JSP<br />Office<br />ADFSwing<br />Mobile<br />View<br />ADF Faces<br /> JSF<br />Struts<br />ADF Controller<br />Controller<br />ADF Binding<br />Model<br />Business Services<br />EJB<br />BAM<br />ADFbc<br />Portlets<br />BI<br />BPEL<br />Web Services<br />Java<br />Data Services<br />Database<br />Web Services<br />Legacy Systems<br />Apps Unlimited<br />
    3. 3. “We could also do that in the database”<br />in the database? Huh?<br />RDBMS<br />≈<br />
    4. 4. Design & team that combines strengths of all technologies…<br /><ul><li>Ease and Elegance of Implementation
    5. 5. Functionality (in an affordable way)
    6. 6. Productivity
    7. 7. Performance</li></ul>ADF Controller/<br />ADF Faces<br />ADF Model<br />ADF BC/JPA/WS*<br />Oracle RDBMS<br />
    8. 8. Database Strengths<br />Integrity<br />Fine grained (data) security and auditing<br />Data Retrieval <br />joining tables together, leveraging indexes<br />hierarchical, network-like traversals <br />advanced analytics, historical queries, mining <br />Aggregation and Sorting<br />Complex & Massive Data Manipulation<br />
    9. 9. RDBMS not always exclusively accessed through one Java API<br />SOA, ESB, WebServices<br />Database<br />Batch Bulk Processes<br />Standard Applications<br />LegacyApplications<br />Data Replication & Synchronization<br />
    10. 10. Database Triggers – decorating Data Manipulation<br />Triggers execute before or after Insert, Update or Delete of database records<br />insert, update, delete<br />Before Insert trigger: sal=…<br />Employees<br />
    11. 11. Purpose of triggers<br />Set default values on new records<br />if :new.job=‘SALESMAN’ then :new.comm = 1000<br />Calculate & Derive values upon insert, update or delete<br />Notify third parties of data manipulation<br />Perform complex validation on the data changes applied by the transaction<br />Per Department: Max Salary < 1.8 * Average<br />Per Manager: #subordinates < 15<br />
    12. 12. ADF BC refreshing Entity Objects after triggers have applied new values<br />Entity Object Employee<br />(leveraging: returning <column> into : bind)<br />post<br />Before Insert trigger: sal=…<br />Employees<br />
    13. 13. Aggregation & Rollup<br />Data for reporting purposes can be prepared by database queries<br />Including aggregations(max/min/avg/count/sum)<br />and Sub Totals <br />and Grand Total<br />and String Aggregation<br />
    14. 14. Dynamic Aggregation through Non-conventional use of bind parameters<br />Bind parameters are typically used in the WHERE clause of a SQL query<br />However: they can also be used in the SELECT, ORDER BY and GROUP BY sections<br />A combination of CASE and bind parameters in the GROUP BY can provide interesting options<br />DynamicAggregation<br />
    15. 15. Query for dynamic aggregation<br />
    16. 16. Sub and Grandtotals with Rollup<br />Rollup instructs databaseto aggregate at every levelstarting from the right<br />deptno, job<br />deptno<br />(grand total)<br />Also see:<br />Cube<br />GroupingSets<br />
    17. 17. Leveraging SQL Aggregation to make life easier for the UI developer<br />
    18. 18. Analytical Functions – spreadsheet-style row processing<br />Analytical Functions allow SQL queries to perform inter-row comparison & aggregation<br />For example: in a single query, for each employee<br />show salary rank in department and job<br />show salary difference with colleague next higher in rank (on the list per department)<br />show average salary in the department<br />show csv list of colleagues in department<br />
    19. 19. Analytical Functions - example<br />
    20. 20. Flashback Query<br />select emp.*, dept.dname<br />from emp AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '1' DAY) <br />, deptwhere emp.deptno = dept.deptno<br />
    21. 21. Show historic situation for selected records<br />
    22. 22. Flashback Versions<br />Retrieve all states each record has been in<br />Every transaction that touched a row left a version of the record<br />Pseudocolumns: xid, operation, start time, end time<br />Use constants minvalueand maxvalueto retrieve all versions<br />Flashback versions make journaling tables redundant<br />
    23. 23. Employee Version-history with Flashback Query<br />
    24. 24. Show change history for a record based on Flashback versions<br />
    25. 25. Embedding Historic Data in ADF Application<br />Where Clause in (read only) ViewObject can include FLASHBACK operators<br />AS OF and VERSIONS BETWEEN<br />Bind parameters can be used to set the point in time or the historic time interval<br />A time selector can be used to visually set the interval<br />Scalar subqueries can be used for ‘in line comparison to a certain point in time’<br />“How much higher/lower is the salary than at the selected date?”<br />
    26. 26. Trees<br />
    27. 27. Trees<br />
    28. 28. ADF Model & Tree Data Binding<br />Create hierarchical relation between multiple ViewObject or (POJO) Collection Bindings<br />Tree Data Retrieval retrieves collections in several steps i.e. multiple queries<br />Data is cached<br />Data is only queried when required (given the expansion level of the tree)<br />Alternatively: have the database do the heavy tree lifting: Database has optimized tree algorithms<br />Starting at any node in the tree or network<br />Drilling down to the specified number of levels<br />Order siblings within parent<br />Indicate leaf and parent nodes; detect cycles<br />
    29. 29. Retrieving Hierarchical data sets with single SQL statements<br />Database has optimized algorithms<br />Starting at any node in the tree or network<br />Drilling down to the specified number of levels<br />Order siblings within parent<br />Indicate leaf and parent nodes; detect cycles<br />EMPID ENAME MGR DEPTNO LEVEL<br />--------------- ---------- ---------- ---------- ----------<br /> 7839 KING 10 1<br /> 7698 BLAKE 7839 30 2<br /> 7499 ALLEN 7698 30 3<br /> 7900 JAMES 7698 30 3<br /> 7654 MARTIN 7698 30 3<br /> 7844 TURNER 7698 30 3<br /> 7521 WARD 7698 30 3<br /> 7782 CLARK 7839 10 2<br /> 7934 MILLER 7782 10 3<br />
    30. 30. Oracle 11g and ANSI SQL for hierarchical query<br />with employees (empno, name, mgr, hierlevel, path) as <br />( select empno, ename, mgr, 1, ename<br /> from emp<br /> where mgr is null<br /> union all<br /> select e.empno, e.ename<br /> , e.mgr, m.hierlevel + 1<br /> , m.path||'/'||e.ename<br /> from emp e<br /> join<br /> employees m<br /> on (m.empno = e.mgr) <br />)<br />select *<br />from employees<br />
    31. 31. Filter-driven querying<br />FilteredEmp<br />
    32. 32. Steps for filter driven querying<br />Determine the values to filter on<br />Create a query to retrieve for all filters<br />Every individual value and the # occurrences<br />The where-clause to apply on the real VO<br />The label for the filter<br />Create a managed bean to apply selected filtersto ViewObject<br />Create page that displays filters and selected dataand handles filter “clicks”<br />
    33. 33. Encapsulate Database specific SQL in a View API<br />Views – for encapsulation of data model, multi-table join, (advanced) SQL hiding, authorization rules<br />Note: a view looks like a table to the client<br />View<br />
    34. 34. The read-only cursor API<br />A Cursor is a reference to a query result set<br />Database can open a cursor for a SQL query<br />And return it to the application to fetch the rows from<br />Cursor == JDBCResultSet<br />A cursor can be nested: containdetails … <br />JDBC ResultSet<br />while rs.next { … }<br />cursor<br />Stored Procedure<br />Departments<br />SQL<br />Employees<br />
    35. 35. Cursor for Master-Detail resultset<br />Stored Procedure<br />
    36. 36. Using Complex Views for Hiding Legacy Data Models<br />
    37. 37. Providing a ‘business object’ API<br />DML API: a View – aided by an Instead Of trigger<br />Insert of one new row inUSERS_VW (e.g. a JPApersist operation) can actually be four new records<br />USER, PERSON, EMAIL_TYPEEMAIL_ADDRESS<br />USERS<br />USERS<br />EMAIL_TYPE<br />Instead Of DML trigger<br />*<br />*<br />PERSONS<br />EMAIL_ADDRESSES<br />*<br />*<br />
    38. 38. Instead of Insert Trigger on USERS_VW (1/2)<br />create or replace trigger handle_insert_users_trg<br />instead of insert on users_vw<br />for each row <br />declare<br /> l_psn_id persons.id%type;<br />begin<br /> insert into persons<br /> ( id, first_name, last_name, address, ...)<br /> values<br /> ( nvl(:new.id, central_seq.nextval),:new.first_name , :new.last_name, :new.address, ...)<br /> returning id into l_psn_id;<br /> insert into user_accounts<br /> ( id, psn_id, username, password)<br /> values<br /> ( central_seq.nextval ,l_psn_id , :new.username , :new.password);<br />
    39. 39. Instead of Insert Trigger on USERS_VW (2/2)<br />... <br /> insert into email_addresses<br /> ( id, psn_id, ete_id, address)<br /> values<br /> ( central_seq.nextval , l_psn_id<br /> , ( select id <br /> from email_types ete <br /> where ete.address_type = :new.primary_email_type ) , :new.primary_email)<br /> ;<br />end handle_insert_users_trg;<br />
    40. 40. Creating a new user<br />User Administration<br />USERS<br />First Name<br />Last Name<br />Molly<br />Warhol<br />Username<br />Password<br />mwarhol<br />******<br />USERS<br />EMAIL_TYPE<br />Instead Of DML trigger<br />Address<br />City<br />1 Slickroad<br />Las Vegas<br />Telephone<br />Mobile<br />5551212<br />43219876<br />*<br />Email<br />Email type<br />mw@un.org<br />Business<br />PERSONS<br />EMAIL_ADDRESSES<br />Activation<br />24-may-2008<br />*<br />*<br />
    41. 41. ADF BC and Complex Views with Instead of Triggers<br />Override the lock method in the ViewObjectImpl<br />Default implementation will attempt select … from <view> for update ofWhich is not allowed on Views with an instead of trigger<br />
    42. 42. Do not do it…More often than required<br />Save on network trips, context switches and tiers to cross<br />Save on ‘reproducing’ same results<br />Web Browser<br /><ul><li>JS data (memory)
    43. 43. Cookies
    44. 44. HTML 5 db</li></ul>Edge Cache<br />JEE Application Server<br />Cache<br />Cluster Fail-Over<br />(Session State)<br />Result Store<br />Write Behind<br />Client Result Cache<br />RDBMS<br />Result Cache<br />Materialized View<br />
    45. 45. The Hollywood Principle: Query ResultSet Change Notification<br />POJO / ADF BC<br />
    46. 46. Cache Refresh triggered by DB<br />Oracle RDBMS invokes Java Listener with event details<br />POJO / ADF BC<br />Register DatabaseChangeNotification<br />SQL query<br />PL/SQL<br />
    47. 47. Shared Application Modules<br />Normal Application Module instances are session level – i.e. not shared across (web) sessions<br />Shared Application Module instances are shared across sessions like an Application Scope Managed Bean<br />Used for Static Data Sets: Look Up Data and Reference Tables<br />Sessions can reuse the data from a shared Application Module without having to access the database<br />And loading the same data in session level memory scope<br />View Accessors can be used to access data in the Shared Application Module’s VOs<br />For example for use in LOVs or Validation Rules<br />
    48. 48. Shared Application Module Instance<br />
    49. 49. Auto Refresh for ViewObjects<br />ViewObjects in a Shared Application Module can be registered for auto refresh<br />Typically such application wide VOs are near-static<br />Whenever the underlying data set changes (in the database), the VO rowset should be refreshed<br />By setting Auto Refresh (to true) for the ViewObject, the VO will be refreshed whenever the database is changed<br />ADF registers the VO query with the Database (11g) Result Set Change Notification mechanism through JDBC<br />Note: the VO should not have an Order By clause nor select a Date column<br />
    50. 50. Steps for auto refresh enabling<br />Create Shared Application Module<br />New application module that is added to list of Application Level instances in the Project properties<br />Create the ViewObject that queries the ‘static data’ and add to Shared Application Module<br />Set the Auto Refresh property to true for VO instance<br />Database must be 11g (and have parameter compatible set to 11.1.0 or above)<br />database user must have the Change Notification privilege<br />To piggyback changes to page, set changeEventPolicy to autoPPR on data binding<br />
    51. 51. Set Auto Refresh for ViewObject<br />Set Auto Refresh for ViewObject<br />Grant Change Notification todatabase user<br />
    52. 52. Demo<br />DBQRCN<br />
    53. 53. Reaching out from the database<br />Database<br />
    54. 54. Email conversations<br />
    55. 55. Database receiving and sending emails – from people or applications<br />
    56. 56. RESTful resource navigation<br />
    57. 57. RESTful architecture<br />http<br />http<br />http<br />RESTful PL/SQL APIexposed through dbms_epg<br />
    58. 58. JEE Application Server<br />Enterprise Service Bus<br />ADF Application<br />Web Service<br />?<br />Database informing and leveraging the middle tier<br />HTTP calls using the UTL_HTTP package<br />
    59. 59. Asynchronous processing<br />Execute stored procedures or command scripts (O/S) in the background – using a job<br />Free up the synchronous thread<br />Return control to invoking Java application<br />Ideal for Fire-and-Forget (one way) calls<br />Results can be returned asynchronously<br />Via Queue, Table, Database Query Result Change Notification, HTTP call, Email,…<br />Create a Job in the Oracle Database using:<br />package dbms_scheduler<br />
    60. 60. Other Database Features worth investigating <br />Virtual Private Database & Fine Grained Authorization<br />XMLType, XMLDB & FTP/HTTP/WEBDAV server<br />Object Types and Collections<br />Data type Interval & Time Zone support<br />Fine Grained Auditing<br />System Triggers, for example “after logon”<br />(Global) Application Context<br />Autonomous Transaction<br />Advanced Queuing (& JMS interaction)<br />Creating advanced job execution schedules<br />Edition Based Redefinition (versioning of database objects)<br />Statistics and Data Mining<br />Scalar Subqueries<br />
    61. 61. Summary & Conclusions<br />Databases can do much more than<br />ADF applications can benefit!<br />Strike the right balance:<br />Leverage database forwhat it can do best<br />Make ADF and Database work together in a smooth way<br />

    ×