July 29, 2004

Enterprise JavaBeans 101 - Entity Beans

Introduction


In the first installment in the EJB 101 series, I provided a brief overview of Enterprise JavaBeans (EJB) technology followed by an introduction of one of the EJB types � the Session Bean. In this installment, I will discuss another type of EJB, the Entity Bean. Specifically, I provide an overview of the different types and then describe their relevance within Excellus.

Entity Beans

Entity Beans are another type of EJB that provide interaction various data sources on behalf of a client program. One can think of Entity Beans as the I/O modules of the Java world. There are two types of Entity Beans. The first type is the Container-Managed Persistence (CMP) Bean and the second is the Bean-Managed Persistence (BMP) bean. I will describe each of those in the following sections.

CMP Beans

War Story

In a previous job with a now remnant of a dot-com company, we did much of our web development with proverbial spears and stones. Specifically, we wrote our applications with the C language in order to maximize portability between disparate UNIX platforms and to ensure performance would be acceptable. We created and supported a large public job search database so performance was foremost on our minds.[1]

One of my responsibilities was to be the keeper of the data model and to maintain a core library of �classes� that could be used for higher-level functionality. A key part of this library was what we called the rdb (relational database) series of modules that we wrote for each type. Each rdb module was capable of performing all the key database operations (select, insert, update, delete) with the particular type. If we had a data type called customer_t, we would have a customer_rdb module to support the I/O operations to the database. At first, we hand wrote this code. That worked for the first six to ten data types. Then our hands started to hurt from all the typing! Furthermore, since we were using a lower-level language like C, the propensity to introduce errors into the code was much higher especially when working sixty hours a week fueled solely by pizza, caffeine, and the occasional chicken vindaloo.

After the first release of a particular product, we decided that there was a better way to make a living. We also realized that we were going to have nearly 100 data types in a future release of the product so we needed a solid strategy to save our fingers from cramping on the keyboard.

Enter my buddy D. D was many years my junior, wore his hair much longer than mine and was really, really smart. Smart like he could do hexadecimal math in his head and recite almost a hundred places for PI. D created a way to read the database catalog tables and generate the necessary code to perform the database operations. It was really slick! All I needed to do was to modify the data model, generated the SQL code and then modify the database. Once those tasks were complete I ran D�s magic code generator and POOF! out came the code ready to compile and distribute to the rest of the development team.

Back to Reality

With CMP beans, we have the same advantage that I had with D�s magic code generator except there are many D�s working within companies like IBM solely devoted to the task of writing the code generators. With CMPs, the developer is not required does not write the code to select, insert, update, or delete an object from a database. Rather, the developer configures the bean using a series of IDE wizards and generates what is known as deployment code. The benefit of this approach is the developer is liberated from having to write repetitive (and thus error-prone) I/O code. Not only does this practice make for a happy WebSphere Application Server (WAS) administrator, but it also makes for happy database administrators (DBA) because us pesky Java folks aren�t hogging all the database threads with bad code. Another benefit is that it�s just plain faster to create a CMP bean to interact with one or more database tables.

The disadvantage with CMP beans is that the developer has less control over what is happening. Basically, the developer is at the mercy of the application server vendor to create proper database I/O code. Also, in some limited cases, CMPs do not fulfill the functional requirements of a particular aspect of a system. Fortunate for us, there is still a way to hand-code a solution using something called a BMP bean.

BMP Beans

MP beans are still first-class EJBs in that they are eligible for nearly the same manageability features of other EJB types. However, they require the developer to code all interactions with the data source. In some respects, we travel back prior to the creation of D�s magic code generator. However, there may be valid reasons for the developer to choose BMP beans. For instance in order to optimize database access, accommodate intricate search requirements, or deal with non-relational data stores such as a flat file, IMS database, or other persistent data store. The developer may also need to integrate a non-EJB I/O class into an EJB framework for proper transaction and/or security control (more on these features in a future article).

Like CMP beans, BMP beans have a particular way of being written and the literature on the topic spells out exactly how this should be done. The developer is not left to his own devices to create random bits of code and hope it works.

Entity Beans in the Field

Within Excellus, two projects are currently using CMP beans while another project is using both CMP and BMP beans. The PIMS and Contract Development System (CDS) use CMP beans as the primary mechanism for RDBMS I/O. The Product Central system uses a mix of BMP and CMP beans for persistence.

Regardless of type, Entity Beans are generally used within a Session Bean. The rationale behind this approach is to shield the client layer of the application (usually the presentation layer) from the intricacies of the database that stores the data. By employing this type of layers technique, one dramatically increases the maintainability of a system. This maintainability is achieved by allowing database structures to vary without adversely affecting other layers of the application. There are other benefits to this approach described in [4].

Summary

In this article, I provided a very brief overview of Entity Beans. I described the two types of beans, the CMP and the BMP bean, while including some rationale around the choice of each type. In the next article, I will describe Message-Driven Beans that can be used with asynchronous messaging systems like WebSphere MQ messages.

If you have additional questions or are interested in more information on how your team can begin learning J2EE technology, please contact me at the email address listed below. I would be happy to visit teams to discuss any J2EE-related topic in further detail.

References

1. Shirah, Joe Sam. Getting Started with Enterprise JavaBeans Technology. http://ibm.com/developerworks/java/edu/j-dw-java-gsejb-i.html.

2. Sun Microsystems. Enterprise JavaBeans 2.0 Specification. http://java.sun.com/j2ee/1.3/docs.

3. Roman, Ambler, Jewell. Mastering Enterprise JavaBeans 2/e. Available in the Excellus iSpace Teamroom.

4. Sun Microsystems. Session Fa�ade Design Pattern. http://java.sun.com/blueprints/corej2eepatterns/Patterns/SessionFacade.html.


July 27, 2004

Sabattis Log - The Contract

One of the issues that I addressed with the boys prior to leaving was the seriousness of the bear situation at Sabattis. Having been to Sabattis as a Boy Scout in the seventies and talking to my brother (a 46er), I knew that it was a serious situation. No one could have food or anything with scent in their tent, ever. Food spills needed to be cleaned up immediately. Camp rules indicated that all food was "bear boxed" by 1900 each night. We cheated a bit by keeping our vehicles in the camp site during the week to avoid any mixups with bear boxes.

Although I was even a bit tense regarding the bears, I soon realized that there is an implicit contract between the bears and the humans. "Give me the peanut butter and no one gets hurt" is the general rule of thumb with the bears. My fears were dismissed when I saw a bear just outside my tent. He wasn't phased by me or any of the others at the campsite. The deer were equally unphased by the presence of humans; one or two were just outside Kevin's tent one morning. One even came onto the archery range and was unnerved by Ken running out to "shoo" her away. For a moment, the deer appeared so calm that I thought Ken was going to give her a hug!

Don't get me wrong, the animals are to be taken seriously, especially the bears. I think the other rule of thumb is: the bear always wins!

Sabattis Log 16 July 2004

Mark, our Scoutmaster, is a former Air Force and National Guard soldier who worked as part of the ground crew for fighter pilots. Pilots use "call-signs" or nicknames not only for security reasons but because acquiring a nickname symbolizes acceptance into the fraternity of aviators.

Today, we went through a similar process. By this point, us leaders are really doing much less leading which is the way things are supposed to be. For some reason, I feel less like a distant leader but more like just one of the brothers. Anyhoo, we did assign nicknames to the boys today. I will not list the names here because I don't want to expose the boy's names to those on the 'net who would misuse the information. However, I will summarize some of the rules that we made up (on the fly) to administer the nicknames

  1. One cannot assign a nickname to oneself. The scout brothers will do that.
  2. The nickname cannot be degrading to the individual.
  3. The nickname will most likely annoy one's mother. Oh well...
  4. The nickname is intended for use by one's scout brothers. For example, one's kid sister cannot use it. Its a brotherhood thing...

Sabattis Log 15 July 2004

Yesterday was a lazy day for the leaders. I personally didn't sleep that well on the island last night although Joe did quite well with his 6"+ air matress. I fulfilled part of the Scoutmaster merit badge requirement by taking both a morning and afternoon nap. There was a water carnival which consisted of water baseball; hitters would 'run' to the bases on the docks and then into the water. Picture a T-shaped dock in the shallower areas with an isolated floating dock approximately 25' further out in the pond. Shortly after the event we split up between the rifle range (12+ yrs) and the archery range for the younger boys.

Sabattis Log 14 July 2004

Morning started with a very loud bird on Goosneck island around 0530. We left for the island around 1730 the night before with a leisurely 1.75 mile paddle. The weather held for the duration of the trip. Tom, a HS senior, guided our trek to the island with all the boys. Joe joined us after returning from a business trip to FL. Since he was not certified at the waterfront yet, he rode out on the pontoon boat.

We all pitched our provided trail tents in designated sites adorned with dead pine needles thus softening som e of the blow of the hard ground. The island jets out to the west with a thick covering of coniferous vegetation leading to a cliff preceded by a fire square with logs and a fire pit. This would serve as an area for a brief amount of fellowship until the bugs carried us to our tents for the evening. If the weather and bugs were more favorable, sleeping in the fire square would've have been most attractive. Three of our boys were not afforded the luxury of nylon and mesh tents; they were working on their Wilderness Survival merit badge. This badge requires the boy to construct a shelter from what he can find in the woods and sleep in it overnight. The boys were successful with this task despite a mild amount of dehydration and plethora of bug bites. I was mighty proud of the boys myself as I remember doing this task on a wet November afternoon/night. Nick's shelter collapsed so I don't think it counts. Nonetheless, he and the other boys are very interested in returning to the woods to repeat the exercise for improvement's sake. Our less adventurous boys were not successful with fire building. Mental note taken for future outings! After a brief snack the fire was extinguished and we all went to our tents around 2100.

Because we didn't have water filtration with us, we were all limited with our fluids. The water was converted to bug juice in an old plastic container that you simply didn't look at when filling your Nalgene.

The canoe ride back was peaceful and surely better than the usual commute that I do nearly every day on the NYS Thruway from Baldwinsville to Rochester. The sky began to break as Drew and I paddled into the trek center that morning. He seems quite comfortable in a canoe and did very well with the paddling. I felt refreshed and ready for a new day despite the large amount of tossing and turning the night before from sleeping on a Thermarest-style pad the night before

Sabattis Log 12 July 2004

I see leaders emerging from our cadre of boys. Or are they just men that can't drive yet? Anyhoo, one of the boys sits quietly during some free time to build a coup stick or perhaps his own walking stick. He is embracing the scouting experience - its his own now, not just something he does at his parent's or Scoutmasters' nagging. This is one of the goals that we as leaders trive towards with any endeavor. When the mentored begins to take ownership of what is being taught then we know that true learning has begun.

Overnight, we didn't see or hear any bears in the site. I think the onslaught of hundreds of scouts has scared away most of our black, furry buddies. However, some of the boys observed two deer behind Kevin's tent two tents down from mine.

Drew moved out into Ethan and Syd's tent last night. Based on our 2.5 mile hike yesterday, I can understand why there wasn't any noise coming from the tent by the time us leaders went to bed.

The only industrialized sound emminating from the camp is the sound of the generator at "main rez". In fact, we could hear it from Lookout Mtn when we were hiking on Sunday. Aside from the generator and the occasional high flying aircraft, the camp is void of modern devices which makes this trip salve for the soul.

23:30 We just settled in from a very wet afternoon which started with an aborted tour of the various islands and other trek destinations that are available. Our original plan was to spend the night on Frying pan islad. Since the rain was so heavy, we decided to just go on a quick paddle trip of the islands. Despite the abundance of rain prior to our departure, the rain stopped just as we entered the water from the trek center. What compensated us for the soaked boots and soggy sneakers was a spectacular sighting of a loon that makes its home on the pond. A second loon swan peacefully about the same distance from our canoes. Despite the number of canoes and boys, the loon stayed in its location for quite some time while we observed and snapped a few photos. I can see why various Adirondack nick-nacks use the loon for decorating.

Sabattis Log 11 July 2004

Morning broke with the sounds of Drew watering a fern outside the tent and the hustle of Basko and Bender trying to catch yet another bass from the post. At this time Drew also spotted another bear behind Weller?s truck.

Since we arrived a day early, Sunday's activities consisted of a 2.5 mile hike up and down Lookout Point which is one of the features trek activities at Sabattis. We started by hiking through the soon-to-be-full camp and pausing at the closing campfire location overlooking Little Hornet Pond. Rather than waste time searching for the scantly marked trail, we shose to navigate soley by map & compass to the top of the point. Weller & Sleight took the bearings and up we went to the point. It was nearly straight up on hands and knees through moss covered rocks and fields of dew-covered ferns. Being in perhaps the worst shape of the group, I brought up the rear but still felt suprisingly good for the lack of exercise in my everyday life. Drew did very well and didn't even flinch at the elevation. We then observed the pleasant Adirondack view (and one bear dropping) consisting of well-treed peaks decorating the skyline. The weather was partly cloudy so the crawl to the peak was worth it. After Mitchell "stepped aside" for a moment using leaves for cleanup, we made our way for the real trail at my insistence in order to avoid blowing out my knee going downhill.

The time is 23:16 and we just returned from a number of activities with the boys including a troop photo, knife & axe briefing, and the opening ceremony. Given the wide age range of scouts in attendance, the campfire antics were met with some mocking due to the high level of "corn", however, this ASM appreciated the animated presentation techniques. Mark & I also attended a leader meeting where I realized that Sabattis is not just about collecting merit badges but seeing the hundreds of acres of God's beautiful handiwork. Since it was Sunday we also attended a "Scout's Own" religious service that was diluted and void of any spirituality. In retrospect, we should've have chosen the Jewish service and did mention our disappointment with the service. Apparently, it was thrown together at the last minute because of some last minute staffing changes. No need to worry, our boys have a solid sense of discernment.

Sabattis Log 10 July 2004

We set camp at the Tanglebush campsite at the Sabattis Scout Reservation just outside Long Lake NY approximately at 1700. As the boys selected their cira-WWII style platform tents I quietly grabbed one with a treed view of Three Pound Pond (see Sabattis Scout Reservation). Mine and Drew?s tent (at least for the first night) was within ten feet of the mess fly that our two patrols would share for the duration of our troop?s stay. The pond contains a number of sizeable largemouth bass based on some of the reports by our resident fishing experts Bender and Basko. While Drew played basketball at the trading post I had a chance to view the sunset and listen to the quiet of the mountains accented by one or more loons that make their residence at the pond behind our campsite.

Shortly after the arrival and collection of anything with sent destined for the bear-box, the boys spotted their first Black Bear rummaging throw another adjacent campsite. I think this gave additional credence to the fact that the boys need to be careful of the bears. However, it appears so far that there is just one bear that occasionally visits. As Drew & I fell asleep, we hear the melodic sound of a loon and several coyotes in the darkness. Drew was a bit apprehensive his first night because of the bugs, bears, and other animals, but then again, so was I...

Sabattis Log

My son and I had the privilege to attend Sabattis Scout Reservation the week of 11 July 2004. The blog entries that follow are from my moisture-soaked paper notebook and what I can recall. Enjoy!