|By Daniel O'Connor||
|June 1, 2000 12:00 AM EDT||
Say you're writing an Enterprise JavaBean that represents a persistent object, such as a customer or a product. You have two choices for getting data (such as customer name and product number) from the bean to the database and back:
- You can let the bean's runtime environment its container, in EJB speak do the heavy lifting for you....
- ....or you can provide the logic yourself along with your bean.
According to the current version of the Enterprise JavaBeans Specification (v1.1), a compliant EJB container isn't required to provide any support for mapping the container-managed fields to a database schema. For instance, it could use Java serialization to save the beans to a file. Most if not all commercial and open-source containers will map fields to table columns in a database. But some will do it better than others. You'll have trouble with some products, for example, if you want to map your bean to multiple rows in multiple tables.
In fact, you probably do want to map your bean to multiple rows in multiple tables. Your beans should be coarse-grained, managing their own dependent objects. Your order bean should have line items implemented as a helper class, rather than as references to line-item EJBs. Your customer bean should have addresses rather than references to address beans. The overhead of bean-to-bean calls between an order and its line items, or a customer and its addresses, would be prohibitive. Your EJB server can provide you with many services, such as declarative transactions, security, and even load balancing and failover. But there's no free lunch, and the price you pay is indirection. Every call you make goes through a layer whose purpose is to provide these services (see Figure 1). Managing dependent objects reduces the frequency of trips through this indirection layer (see Figure 2). And a bean with multiple dependent objects needs to be stored in multiple tables, in multiple rows, if you want to maintain a normalized schema.
Technically, bean-managed persistence doesn't mean you have to write your own database access code. It just means the bean, rather than the container, provides the persistence logic. Several good object-relational mapping tools are on the market, and they can be portable from server to server along with your bean. But you may find it impossible to use these tools because of cost (some have runtime fees and/or a hefty per-developer price tag), distribution practicalities or reasons of your own. This article will tell you what you need to do to write your own persistence for a coarse-grained entity bean.
Requirements for Dependent Objects
An efficient implementation of bean-managed persistence for dependent objects will have two features:
- Partitioned storage logic
Load-on-demand means that the dependent objects aren't loaded until they're actually needed. The EJB framework will call a function in your entity bean to indicate that persistent data should be made available to the bean's business logic. The bean could load the dependent data at this point. But if the business logic doesn't make use of certain dependent data during the current transaction, that database access was wasted. For instance, changing a customer's credit rating may not require access to any address, so the addresses shouldn't be loaded. If the dependent data is accessed, it can be loaded at that time. (This is also known as lazy loading.) Partitioned storage logic is necessary so that the bean updates the database the way a relational database expects: new data is inserted, changed data is updated, discarded data is deleted and unchanged data is left alone (see Figure 3). The alternative wiping out the records and reinserting them is too horrible even to contemplate.
A Good Idiom
To implement load-on-demand, you could scatter calls throughout your business logic to functions with names like "ensureAddressListLoaded" and "ensureLineItemListLoaded" that is, if you want to be the poster child for ugly code. And to store your dependent objects to the database, you could have each object keep track of its status: NEW, UPDATED, DELETED or UNMODIFIED. As you totaled the line items in a purchase order, you'd need to check each object to see if it had been deleted (see Figure 4). Don't forget, or you're going to have some unhappy customers.
A better idiom is to group the logic related to persistence with a collection class. Your business logic for an order probably works with a list of line items. To delete an object, the most natural thing to do is probably to remove it from the list. To add an object, the most natural thing is to append it to the list. And simply calling a method on the list should be the signal to your persistence logic that the items in the list need to be loaded from the database. If you use a smart list that knows how to do these things, nothing else needs to be done from the perspective of the business logic programmer. In the example of totaling line items in a purchase order, you'd simply iterate through the objects in the list (see Figure 5).
Behind the scenes, a smart list implementation is keeping track of an "isLoaded" variable. When the list is accessed, it checks this variable first to see if the data needs to be loaded from the database. If so, it loads it. It keeps a set of references to all the objects it loads to distinguish them from new objects added to the list. If an object is removed from the list, it's added to an internal list of deleted objects. This deleted objects list is used by the persistence logic, but not, typically, by the business logic programmer.
The Proxy List
The smart list needs a layer of indirection between the list interface and the actual list storage. At this layer a method call on the list to remove objects will first add those objects to the internal deleted objects list. Also, a call to any function will check to see if the list data has been loaded. One possible way to gain this indirection is simply to implement the java.util.List interface and delegate the calls to a private internal "backing" list. A more elegant way is to use the java.lang.reflect.Proxy class newly available in JDK 1.3. (Note: Using the Proxy class limits your bean's portability to servers that support JDK 1.3. All the techniques and code discussed in this article are easily adapted to the "delegation list" compatible with earlier JDKs.)
The Proxy class dynamically creates an implementation of an interface that will automatically forward all its calls to a middle layer called InvocationHandler (also in package java.lang.reflect). In a subclass of InvocationHandler you can forward the method call (or not), take action before or after forwarding it, alter its parameters and change the returned object (see Figure 6). As you can see, this is more than enough functionality to implement our smart list. The uses for this Proxy are many: it can be used to handle user interface events and to provide a "poor man's multiple inheritance," and has even been used to implement an open-source EJB server, EJBoss. (For more information on the Proxy and InvocationHandler classes, see the article at http://java.sun.com/products/jfc/tsc/articles/generic-listener2/index.html on Sun's Web site.)
Take a look at ListInvocationHandler (see Listing 1). It's the smart list implementation that keeps track of deleted objects, the set of original objects and whether the data has been loaded from the database. It also takes as a constructor parameter its "backing" list so that any class implementing the List interface (LinkedList or ArrayList) can be used, depending on how the data is typically accessed. The main functionality is in the invoke method. Here I check to make sure the data has been loaded from the database. I also check for any List function that removes an object so I can make a copy to use for calling "delete" later on the database. An important point: several List functions will return a reference to the backing list unless these too are interposed on. Any method that returns an Iterator (which points to the backing list) must instead be made to return an Iterator pointing to the interposed list. I did this using you guessed it another Proxy (see Listing 2). Any method that returns a Collection must either be interposed on or made unmodifiable.
My smart list implementation uses two persistence-specific interfaces (see Figure 7). The first, PersistentOperations (see Listing 3), is implemented by the smart list itself (in ListInvocationHandler). It's for list operations needed by the bean's persistence plumbing, rather than the business logic. You can get a list of deleted objects to actually delete them. You can get the set of original objects to decide between insert and update operations. You can add an object to the list so that it's tagged as an original object rather than a new one. You can tell the list that the bean has been asked to save itself to persistent storage, and to take whatever actions are necessary. For symmetry more than need, you can also ask the list to load itself from persistent storage. Typically, you'll let the list decide this for itself.
The second persistence-specific interface used by the list is DataStore (see Listing 4). Although your business logic can treat the smart list as a regular list, you need to put the SQL somewhere, and this interface is the gateway to that "somewhere." Your bean will pass an implementation of this interface to the smart list factory (DemandListFactory; see Listing 5). When the list needs to save or restore its data, it will call methods in this interface, passing a reference to its PersistentOperations interface.
A simplified example of using a proxy list with an EJB with bean-managed persistence is available on the Java Developer's Journal Web site. The bean implements a customer-has-line-items model. A customer has an ID and a name, and an unlimited number of line items of products he or she has ordered. In order to give the smart list a workout, I've written functions so the business logic programmer can add, delete or change a line item, and use the entire set of line items at once. The list is initialized when the bean is created (a new customer record is inserted) or loaded (an existing customer record is read from the database). An anonymous DataStore implementation is passed to the DemandListFactory, which will call bean methods whenever DataStore methods are called by the list.
One bean method will do a simple select on the database table where line items are stored. As it iterates through the result set, it calls the PersistentOperations's addFromStore method, which will indicate to the list that the object already exists in the database and needs to be updated, not inserted, when the list is stored. Another bean method that stores the list is only slightly more complicated. It must use the information available from the PersistentOperations interface to partition the objects into three sets: insertions, updates and deletes. You'll notice that I'm using an isModified function to further partition updates from unmodified instances. It's possible to do this in the smart list as well by keeping a copy of each original object and then comparing it to the object's state just before the database update. There are disadvantages to this technique, however, depending on the memory required to keep copies of those objects and the processing time required to compare object states. In any case, implementing this is beyond the scope of this article.
To test my Customer implementation, I've included a stateless session EJB that will give it a good workout. Fronting an entity EJB with a "business process" session, EJB is a common design pattern. Here it allows us to include multiple adds, deletes, updates and totals within one transaction. Since the typical EJB server will load and store persistent data on transaction boundaries (load when the transaction begins and store when it ends), this is important for our testing. Obviously, the stateless session EJB isn't a good example of an actual business process.
It isn't difficult to write an efficient, easy-to-use Enterprise JavaBean using bean-managed persistence. Well, what I should really say is that it's not much harder than writing the SQL code that your bean will use. If you need to roll your own persistence for EJBs, there's no better place for the persistence logic than a collection class. In this case we used a list, but the same technique would work for a Set or a Map, or even a tree document, depending on your needs. And if your target market allows you to write to the JDK 1.3, the new Proxy class can increase the expressiveness and effectiveness of your code.
The 19th International Cloud Expo has announced that its Call for Papers is open. Cloud Expo, to be held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA, brings together Cloud Computing, Big Data, Internet of Things, DevOps, Digital Transformation, Microservices and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding business opportuni...
Aug. 26, 2016 07:00 AM EDT Reads: 3,904
19th Cloud Expo, taking place November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA, will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud strategy. Meanwhile, 94% of enterpri...
Aug. 26, 2016 05:00 AM EDT Reads: 3,021
Personalization has long been the holy grail of marketing. Simply stated, communicate the most relevant offer to the right person and you will increase sales. To achieve this, you must understand the individual. Consequently, digital marketers developed many ways to gather and leverage customer information to deliver targeted experiences. In his session at @ThingsExpo, Lou Casal, Founder and Principal Consultant at Practicala, discussed how the Internet of Things (IoT) has accelerated our abil...
Aug. 26, 2016 02:15 AM EDT Reads: 1,947
With so much going on in this space you could be forgiven for thinking you were always working with yesterday’s technologies. So much change, so quickly. What do you do if you have to build a solution from the ground up that is expected to live in the field for at least 5-10 years? This is the challenge we faced when we looked to refresh our existing 10-year-old custom hardware stack to measure the fullness of trash cans and compactors.
Aug. 26, 2016 01:30 AM EDT Reads: 1,664
The emerging Internet of Everything creates tremendous new opportunities for customer engagement and business model innovation. However, enterprises must overcome a number of critical challenges to bring these new solutions to market. In his session at @ThingsExpo, Michael Martin, CTO/CIO at nfrastructure, outlined these key challenges and recommended approaches for overcoming them to achieve speed and agility in the design, development and implementation of Internet of Everything solutions wi...
Aug. 26, 2016 01:00 AM EDT Reads: 1,954
DevOps at Cloud Expo, taking place Nov 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 19th Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long dev...
Aug. 26, 2016 12:45 AM EDT Reads: 2,262
Cloud computing is being adopted in one form or another by 94% of enterprises today. Tens of billions of new devices are being connected to The Internet of Things. And Big Data is driving this bus. An exponential increase is expected in the amount of information being processed, managed, analyzed, and acted upon by enterprise IT. This amazing is not part of some distant future - it is happening today. One report shows a 650% increase in enterprise data by 2020. Other estimates are even higher....
Aug. 26, 2016 12:30 AM EDT Reads: 2,902
I wanted to gather all of my Internet of Things (IOT) blogs into a single blog (that I could later use with my University of San Francisco (USF) Big Data “MBA” course). However as I started to pull these blogs together, I realized that my IOT discussion lacked a vision; it lacked an end point towards which an organization could drive their IOT envisioning, proof of value, app dev, data engineering and data science efforts. And I think that the IOT end point is really quite simple…
Aug. 25, 2016 11:45 PM EDT Reads: 2,287
Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more business becomes digital the more stakeholders are interested in this data including how it relates to business. Some of these people have never used a monitoring tool before. They have a question on their mind like “How is my application doing” but no id...
Aug. 25, 2016 10:15 PM EDT Reads: 1,717
Identity is in everything and customers are looking to their providers to ensure the security of their identities, transactions and data. With the increased reliance on cloud-based services, service providers must build security and trust into their offerings, adding value to customers and improving the user experience. Making identity, security and privacy easy for customers provides a unique advantage over the competition.
Aug. 25, 2016 09:15 PM EDT Reads: 2,207
Is the ongoing quest for agility in the data center forcing you to evaluate how to be a part of infrastructure automation efforts? As organizations evolve toward bimodal IT operations, they are embracing new service delivery models and leveraging virtualization to increase infrastructure agility. Therefore, the network must evolve in parallel to become equally agile. Read this essential piece of Gartner research for recommendations on achieving greater agility.
Aug. 25, 2016 05:15 PM EDT Reads: 704
Smart Cities are here to stay, but for their promise to be delivered, the data they produce must not be put in new siloes. In his session at @ThingsExpo, Mathias Herberts, Co-founder and CTO of Cityzen Data, will deep dive into best practices that will ensure a successful smart city journey.
Aug. 25, 2016 03:30 PM EDT Reads: 1,499
Internet of @ThingsExpo, taking place November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 19th Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devices - comp...
Aug. 25, 2016 02:00 PM EDT Reads: 3,559
SYS-CON Events announced today that Venafi, the Immune System for the Internet™ and the leading provider of Next Generation Trust Protection, will exhibit at @DevOpsSummit at 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Venafi is the Immune System for the Internet™ that protects the foundation of all cybersecurity – cryptographic keys and digital certificates – so they can’t be misused by bad guys in attacks...
Aug. 25, 2016 01:00 PM EDT Reads: 2,602
SYS-CON Events announced today Telecom Reseller has been named “Media Sponsor” of SYS-CON's 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
Aug. 25, 2016 09:00 AM EDT Reads: 591
For basic one-to-one voice or video calling solutions, WebRTC has proven to be a very powerful technology. Although WebRTC’s core functionality is to provide secure, real-time p2p media streaming, leveraging native platform features and server-side components brings up new communication capabilities for web and native mobile applications, allowing for advanced multi-user use cases such as video broadcasting, conferencing, and media recording.
Aug. 25, 2016 08:45 AM EDT Reads: 2,137
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at Cloud Expo, Ed Featherston, a director and senior enterprise architect at Collaborative Consulting, will discuss the key considerations around quality, volume, timeliness, and pedigree that must be dealt with in order to properly fuel that engine.
Aug. 25, 2016 08:30 AM EDT Reads: 1,802
Pulzze Systems was happy to participate in such a premier event and thankful to be receiving the winning investment and global network support from G-Startup Worldwide. It is an exciting time for Pulzze to showcase the effectiveness of innovative technologies and enable them to make the world smarter and better. The reputable contest is held to identify promising startups around the globe that are assured to change the world through their innovative products and disruptive technologies. There w...
Aug. 25, 2016 08:30 AM EDT Reads: 500
Akana has announced the availability of version 8 of its API Management solution. The Akana Platform provides an end-to-end API Management solution for designing, implementing, securing, managing, monitoring, and publishing APIs. It is available as a SaaS platform, on-premises, and as a hybrid deployment. Version 8 introduces a lot of new functionality, all aimed at offering customers the richest API Management capabilities in a way that is easier than ever for API and app developers to use.
Aug. 25, 2016 06:00 AM EDT Reads: 1,454
SYS-CON Events announced today that 910Telecom will exhibit at the 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Housed in the classic Denver Gas & Electric Building, 910 15th St., 910Telecom is a carrier-neutral telecom hotel located in the heart of Denver. Adjacent to CenturyLink, AT&T, and Denver Main, 910Telecom offers connectivity to all major carriers, Internet service providers, Internet backbones and ...
Aug. 25, 2016 02:00 AM EDT Reads: 1,821