Class Cast Exception is a Proxy Problem – Revisited using the Visitor Pattern

In my previous post: ClassCastException is a Proxy Problem I talked about how Hibernate tries to optimize the loading of objects by lazily fetching the parent class object, and returning a proxy for the subclass instead of the actual subclass, thus causing a ClassCastException when the proxy is cast to the actual subclass.  The proxied object doesn’t contain member variables or a representation of the subclass’ methods, only the parent’s.

My proposed solution to the ClassCastException was to add an enum for each subclass that would be stored in the parent class, and used to figure out the subclass of the object and retrieve the actual instance from the database.  I created a method that returned a generic in either the parent class or DAO to keep the code clean.  However, after implementing this solution I discovered a bug that had been introduced a long time ago; the wrong enum value had been stored for the subclass.  The enum value was for SpanishCourse, so when I retrieved the object from the database, I cast it to SpanishCourse, when it was actually a MathCourse, causing a ClassCastException.   While I could just fix the data in the database I wasn’t satisfied with this pattern, because it broke the OO principle of encapsulation; the parent class had to have knowledge of its subclasses’ enum value.

I searched for awhile and found this article: Hibernate Proxies and Polymorphism.  I do think this is a good solution to the proxy problem, but, it pertains to lazily initializing the member variables of a subclass, and is a bit overkill for what I needed.  So then I went back to thinking about a solution for my specific problem.  How was I going to load the subclass instance without it being proxied?  

Here are a few things to note about Hibernate:

  1. Hibernate proxies an object inside a database transaction, the object is lazily loaded and it contains no data.  The data can be lazily fetched once a superclass method is called, as long as the database transaction is still open, otherwise you will get a LazyLoadingException.
  2. When calling a superclass’ abstract method, Hibernate will lookup the implementation of the method corresponding to the appropriate subclass.
  3. Hibernate cannot resolve subclass methods that are not part of the abstract parent class.

Here is my proposed solution:

public abstract class Course {

      public abstract class void load();

      ….

}

Each subclass’ implementation of the load method will handle lazily fetching the member variables of the class that are needed.  This avoids the need for a cast altogether.  If you need to return the object you can continue to use generics.  If you need the actual instance of the subclass then you will need to implement the visitor pattern described in the Hibernate articles.  Here is a simple example:

public interface CourseVisitor {

     public MathCourse getMathCourse();

     public SpanishCourse getSpanishCourse();

     …

}

public abstract class Course {

      public abstract void load(CourseVisitor visitor);

      ….

}

public class MathCourse {

     public void load(CourseVisitor visitor) {

         return visitor.setMathCourse(this);

     }

     …

}

public class CourseVisitorImpl implements CourseVisitor {

     private MathCourse mc;

     private SpanishCourse s;

     public MathCourse getMathCourse() { return mc; }

     public SpanishCourse getSpanishCourse(){ return s; }

     public void setMathCourse(MathCourse mc) { this.mc = mc; }

     public void setSpanishCourse(SpanishCourse s) { this.s = s; } 

     …

}

The Course object will be a proxy, and when the load method is called on it, passing in a new instance of CourseVisitor the subclass will be instantiated, and can be retrieved using the CourseVisitor’s getter method.  

This solution introduces more code, and requires that the CourseVisitor be updated with new subclasses of Course.  However, the visitor pattern is the correct solution to use, because it makes use of language concepts instead of relying on data such as an enum value, which can be error prone.

Post to Twitter Tweet This Post

Her World – Installment 5

Jenna sat looking out of her bay window in her old brownstone.  As the sun’s rays beamed on her face she drifted deep into thought;  this was the place she had hoped to call home again.  She hadn’t had any expectations for her return.  She had always thought of this place as home, because of all the fond memories and experiences that took place here.  During her travels she would think about them, she would imagine herself laying in bed alone and gazing out at the street, or cooking in her kitchen assembling her mis-en-place, and then enjoying the fragrances and aromas of her creations.  She often wondered what it would be like to return, whether she could just pick up from where she had left.  But, now she was there, still sitting, with nothing left to imagine.  Things had changed, her friends had evolved, but so had she.  In the past five years she had gained new perspectives on the world and how she wanted to live.  But in a lot of ways she had stayed the same, and retained her old hopes and dreams.  The dream to come back to her brownstone, to buzz him up, to cook for him, and to lay in bed in his arms, and tell him about all her experiences.  But she knew none of this was possible anymore.  Her dreams would not come true.  She had to fight to get over it.  Her travels had made her stronger, but she still had not gotten over him.  The one love she had had in her life had been over for quite sometime.  

Over the years she stopped blaming herself, but she still felt an emptiness that she hadn’t felt when they were together.  While she tried hard to let others in, it wasn’t the same.  The memories of their life together always interfered with her new found life.  The only place she could connect with others was through art.  She could evoke an emotional response in others and feel a connection as she inspired the uninspired.  She helped design many Kouroi, and while the artists weren’t aware, to her, each one had an element of him in it.  His shoulders, his hands, or his strong legs.  When she was commissioned to help work on the sculpture of a head, she disappeared after designing the eyes and lips, because for the first time in five years she saw him again, and could no longer bare the thought of never seeing this stone sculpture, she wanted to see the live representation of it.  This is what had led her to ending the five years of wandering, and come back home.  But her love was betrothed to another, and she once again felt empty.  She wanted desperately to go to him, she wanted to tell him how she still felt.  But, she knew it would be unfair to him.  But, the night of the rehearsal dinner she she had felt a connection.  She saw how he looked at her.  ”No its only because he hasn’t seen me in so long.  It doesn’t mean anything,” she told herself.  

“Why is this so hard?  Why did Charlie have to leave?”  The only other man next to Aidan that she had come close to falling for had been Charlie, but he wasn’t ready for her.  And his only escape was to runaway.  Perhaps his actions had influenced her to do the same.  The thought of unrequited love was too much for her, and quickly after Charlie’s departure she too ran away.  

***

Drew walked up the staircase and rang the buzzer several times.  There was no answer.  He looked at his watch, he was an hour late, he should have called.  Still where could she be?  He peered into the glass.  It didn’t look like anyone was around.  Drew sat down on the steps and figured he would wait for her.  He wasn’t leaving until he got an explanation, she owed it to him, they were supposed to be best friends!

Then off in the distance a small cheery girl was walking up.  She stopped at the stairs, and looked up at him.  ”Are you Drew?” she asked.  He nodded back at her, “And who might you be?”  

“I’m Priscilla, I’m supposed to deliver this to you.”  She was holding a vanilla colored envelope.  ”My aunt Jenna said its very important!”

He took the envelope from her, and on the front in Jenna’s handwriting it said:  ”To my love, Aidan.”  ”Where did she go?” demanded Drew.  

“I don’t know.   I overheard my dad talk to mom, something about sailing.”  

Drew sighed, he had lost her again.  He would have to wait for her to show up again.

The following day, the wedding ceremonies started.  Drew was hesitant to deliver the envelope to Aidan, he wanted to open it, and find out what had happened, but he was a good friend and respected Jenna, so he kept it sealed.

Aidan was putting on his tux when Drew walked him.  ”Good luck today.” remarked Drew.  ”Thanks!  What have you got there?” asked Aidan noticing the envelope.  

“I’m not sure if this is good or bad, but its for you,” replied Drew handing over the envelope.  

“Oh its from Jenna.”  Aidan’s heart started to race.  He had thought about her constantly, since the night of the rehearsal dinner, and had told Sara that Jenna was in town, but nothing more on the subject, because he knew Sara would get nervous.  He had secretly hoped that she would be at the wedding with Josh, he missed having her around, but he also knew it would make things difficult for everyone, especially her.  He hadn’t anticipated how he would feel if she ever returned.  He had just hoped that she was happy wherever she was in the world.  But the night he saw her again, he knew what she wanted.  And while he loved her immensely and thought the world of her, he knew he couldn’t give her what she wanted, and so desperately needed.  As he started to read her letter, a tear ran down his cheek, he closed it, and thought to himself, “if only…”

Post to Twitter Tweet This Post

Warrior Women

In one of my favorite TV shows of all time Firefly Wash is being intensely interrogated, and the interrogator asks Wash about his relationship with his wife Zoe, Wash responds rhetorically with a look of pride, “Have you ever been with a warrior woman?”   I personally have never been with a warrior woman, but this evening I attended the Anita Borg Awards in San Jose, and was in the presence of a warrior woman, Padmasree Warrior, the CTO of Cisco.  After her keynote speech I knew how Wash felt.  

Lately, I’ve been taking stock of women in engineering that have inspired and challenged me.  My favorite being my Professor at Duke Lisa Huettel.  But, I’ve felt a strange void.  I’ve been looking for someone who symbols almost exactly who I want to be in 10 or 20 years.  Some who is articulate, stylish, and graceful, in essence a femgineer.  And until now no one embodied these traits for me, until I listened and watched Padmasree deliver her keynote.

I’ve attended many keynotes by the likes of Madeleine Albright, Tom Wolfe, and read transcripts of keynotes of Steve Jobs and Carly Fiorina, but I must say this was hands down the best I’ve every experienced in my life!  I left feeling inspired but also light-hearted.   There were two points she made that really spoke to me.  First, Padmasree said that she never understood why people talk about balance as if work and life are constantly at odds with each other.  She insteads focuses on integrating work and life.  Second, she said you shape your opportunities.  I don’t think of myself as a naturally talented coder or having an astronomically high IQ, but I think I make up for those two areas with my drive and ambition, and use those as leverage to land opportunities.

So thank you Ms. Warrior for sharing your drive and courage with me tonight, and being a fabulous femgineer :)

Post to Twitter Tweet This Post

Class Cast Exception is a Proxy Problem

I often face the following exception in Hibernate not on my local developer machine, but in production across several servers:

java.lang.ClassCastException: mint.hibernate.MathCourse$$EnhancerByCGLIB$$ed898f0d

This happens when I try to do something like this:

Set<Courses> courses = getCoursesDao().getCourses(userId);

for (Course c : courses) {
MathCourse mc = (MathCourse) course;
     // do something with mc
}

Assume MathCourse is a subclass of the abstract parent class Course. The gist of the problem is that Hibernate is trying to optimize and avoid going to the database by returning a proxy object. However, a proxy object cannot be downcast to the subclass object, the exception occurs because of downcasting. The solution to the exception is to return the real instance of the object.

One solution is to return the real instance of the object. To do this you could add a method to the DAO to return a generic. Create the function in the DAO class. Then during runtime the generic will be resolved to the appropriate subclass.

public class CourseDao {
public <T extends Course> getGenericCourse() {
     Session session = getCurrentSession();
     Course course = (Course) session.get(Course.class, courseId);
     if (course != null) {
         // type is an enum
        switch (course.type()) {
             case MATH:
                 course = (MathCourse) session.get(MathCourse.class, courseId);
                break;
             …
     }
     …
     return (T) course;
}

Post to Twitter Tweet This Post

15 Ways to Speed Up Your Front-End

Last week I attend the Percona conference in Santa Clara.  The purpose of this conference was to talk about performance related to databases specifically mySQL.  Most of the talks went over my head.  However, there was one talk I attended “Website on Speed” by Philip Tellis from Yahoo! that I thought was outstanding.  In his 20 minute presentation he covered some key optimization techniques to speed up website and improve latency for users, that don’t require time-consuming back-end database restructuring.  Here are some of the ideas I found useful:

 

1. Reduce the number of domains your site has.  Having numerous domains requires a lot of DNS lookups, which makes the site seem slow to users.

2. Avoid redirects or point to a load balancer.  Redirects cause the site loading to slow down user experience.  Load balancers handle request processing by distributing them based on the current capacity of each server.

3. Combine CSS and JavaScript into a single file.  Having a file for each increases the time it takes to download the site content.

4. Combine decorative images into sprites.

5. Cache aggressively.  This of course depends on the content of your site, and how much data is being manipulated by users.  But I would recommend caching static data for as long as possible, which will speed up download time and improve the user’s experience.

6. gZip content over the wire.

7. Minify CSS and JavaScript using a YUI compressor.

8. Reduce cookie size.

9. Post-load components after on-load.

10. Put CSS on top so the browser will download it first, and page has rendered.  This improves user experience, because they can at least start to view the page, even if they can’t interact with it yet.

11. The corollary to #11 is to put JavaScript at the bottom so that it doesn’t block rendering.

12. Avoid using tables for layout in HTML, because you have to wait for the entire table to download before the page can be loaded.

13. Attach events on a container rather than on each element.

14. Use specific HTML elements e.g. unordered list (ul) vs. many divs, this reduces the number of elements that need to be loaded.

15. Profile your JavaScript using YUI profiler, there is also another new profiler called YSlow.

Post to Twitter Tweet This Post

Feather Weight Hibernate Objects

In my previous post Cheap Tricks to Fullfill Your Need for Speed, I talked about how you could reduce the memory footprint of your data base query by using a SQL query instead of a Hibernate query, and retrieve only the columns that are you need. However, there maybe times when you actually want to use a pojo, and retrieving columns and storing them in an object array is insufficient. One solution is to create a feather weight Hibernate object. A feather weight Hibernate object contains a subset of the original Hibernate object’s properties (i.e. a limited set of the data base columns), based on which properties you specify. You still map to the same data base table, but your Hibernate mapping file contains fewer columns, therefore while querying you retrieve data from fewer columns thereby speeding up your query. You can continue to use Hibernate objects as pojos by retrieving the Hibernate through a criteria query or HQL statements.

Another reason to do use feather weight objects is when creating data base tables to store data from third party vendors. In many cases the vendors provide you with a lot of data fields, which you will want to store in the table because your data needs might change based on your feature set. But you might not need to retrieve all the fields for the current feature set. Using feature weight Hibernate objects allows you to continue storing all the fields, but only retrieve those that are the most essential.

Post to Twitter Tweet This Post

Presentation for Code Camp ‘08

Part III. Rapid Development

I’ve covered three of the areas that are very important to becoming a web-service (latency, throughput, and quality), and I’m sure this seems daunting or overwhelming. But keep in mind I’m talking about how Mint’s code and service evolved; we didn’t do everything at once because we did not have the resources or the time. As Mint started maturing there were two areas that we stressed:

  1. Manageability: keeping the code and data base clean, and extensible in case features are cut, added, or revised over time. Its very important to start a project thinking about manageability, or how the feature will evolve within the application.
    • Code manageability: re-factor, don’t introduce a lot of complexity, focus on the tiered architecture to figure out where certain pieces logically fit (e.g. persistence, business logic)
    • Data base manageability:consider how quickly a data set is going to grow when designing tables, foreign key associations, retrieving data, and frequency with which data is accessed.
  2.  

  3. Optimization: improving performance of code at runtime in order to satisfy latency and throughput requirements. While this is important, it is not something that one should focus on from the beginning.
    • Do not make architectural decisions that are too long term, do what you need for the next 6 months. Why? Because its a startup, the product will continue to evolve in approximately 6 month cycles. Don’t waste time optimizing everything, or before you see a demand for a feature. Remember its a startup; resources are scarce and time is critical.
    • E.g. why didnt cache user data we do this from the start? Initially aggregating data nightly because synchronizing data across nodes was difficult and had no mechanism for centralized locking, but once this was put in place we switched to loading data on demand (during user login) and then going through process of aggregating and caching it (in the future we might only show most recent data instead of all data).
    • Why we didn’t shard databases from the start? Huge amount of overhead and engineering resources that needed to be allocated more impending issues.

Post to Twitter Tweet This Post

Cheap Tricks to Fullfill Your Need for Speed

As I’ve mentioned countless times, the performance of a website can really make or break a user’s experience. Everyone places emphasis on tuning and sharding databases, and buying multi-threaded and multi-core processors. These are all viable solutions but they are either pricey or labor intensive. So what are some quick and clean ways to improve the performance of your web app or web service?

Start with the slowest moving part and work from there, disk I/O, or more specifically data base access times. What impacts data base access time? Aside from mechanical parts…slow queries. Ok what are some slow queries? Queries that perform joins and table scans. The biggest and quietest culprit in producing slow queries ORM (Object relationship mapping) software such as Hibernate. Hibernate’s job is to make life easier for Java developers by abstracting away the relationship of a POJO (plain old java object) to a data base object. However, it has a few pitfalls which cause slow queries. Lets explore just two for today:

1. Non-lazy loading of non-nullable objects.
2. Retrieving every column in a row.

If you have a data base object A that has a foreign key relationship to another object B, which is non-nullable Hibernate will load B when only object A is requested. One solution to avoid this is to avoid using a Hibernate query such as the Criteria query, and instead using a straight up JDBC call to retrieve object A.

Suppose only the name field of object A is desired, while this is possible to do in Hibernate, a straight up JDBC call to retrieve a single column from object A is much faster.

Using ORM software apis is easy, but you should understand the price you pay in terms of performance. Use Hibernate queries when you need relationships to be managed amongst objects like when you want to traverse a class hierarchy in Java. But if all you want is a small piece of data using straight JDBC calls are faster and will get you the data you need when you need it.

Post to Twitter Tweet This Post

Presentation for Code Camp ‘08

Part II. Web Application to Web Service

Creating a prototype is very challenging, but its not sufficient. Many companies fail to actually create a service, because they simply take a prototype and add more features to satisfy the demands of the users. Transforming a prototype into a product is what I call the app to service phase, and it takes more than just piling on features. An application is just a point tool that a user uses to complete a few simple tasks, which is why I liken it to a prototype, which is fine if that’s your goal. But if your goal is to create a product that has an ever growing user base then you have to broaden your thinking from features to logistics.

Despite what your product’s goal is there are inevitably three areas that will influence the evolution of you software’s architecture:

1. The growth rate of your user base
2. Data processing and storage
3. Company growth, and more specifically how quickly you grow your engineering team

Post to Twitter Tweet This Post

Trade-offs in Unit Testing – Part II Stubs

After spending a couple days implementing test cases using Mock objects, I switched to testing using stubs, because I still needed to verify the functionality of the code.

It took me only a few hours to write the test cases I needed using stubs, and with and IDE it’s even easier. But I don’t think its a fair assessment to compare using Mocks to Stubs because each tool is used for a different purpose (behavioral vs. functional).

However, I did notice one very obvious disadvantage to using stubs; if your application is dynamic and the functionality is changing test cases become brittle quickly, and require you to add additional members or functions.

But I would still advocate using stubs in writing test cases, because of the simplicity involved in writing them, being able to abstract away or limit dependent classes.

Post to Twitter Tweet This Post