Issue Details (XML | Word | Printable)

Key: HHH-800
Type: Improvement Improvement
Status: Closed Closed
Resolution: Rejected
Priority: Major Major
Assignee: Unassigned
Reporter: Jesse Barnum
Votes: 0
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Hibernate Core

Pluggable handling for LazyInitializationException

Created: 28/Jul/05 05:30 PM   Updated: 03/Aug/05 11:49 AM
Component/s: None
Affects Version/s: 3.0.5
Fix Version/s: None

Time Tracking:
Not Specified

Environment: 3.0.5


 Description  « Hide
I have added 3 very small new classes and slightly modified AbstractPersistentCollection to allow user-created subclasses to easily intercept LazyInitializationExceptions for collections and handle them close to where the problem originates. This does not change existing behavior or API at all.

If it seems like this would be an acceptable modification of Hibernate, I would be happy to make similar modification for single object proxies, as well as individual field proxies.

I don't know if this is the best place to post the code - please let me know if there is a better way to make these kind of submissions.

In AbstractPersistentCollection, modified the following method:

protected final void initialize(boolean writing) {
if (!initialized) {
ProxyInitializer.sharedInstance().initializeCollection( this, initializing, session, getCollectionSnapshot() );
}
}

Created a new class, org.hibernate.ProxyInitializer:
public class ProxyInitializer {
private static ProxyInitializer sharedInstance;

public static ProxyInitializer sharedInstance() {
if( sharedInstance == null ) {
sharedInstance = new ProxyInitializer();
}
return sharedInstance;
}

public static void setSharedInstance( ProxyInitializer initializer ) {
sharedInstance = initializer;
}

public void initializeCollection(PersistentCollection collection, boolean initializing, SessionImplementor session, CollectionSnapshot snapshot) {
if (initializing) {
throw new LazyInitializationException("illegal access to loading collection");
}
if( session == null ) throwSessionMissingException("no session", snapshot);
if( ! session.isOpen() ) throwSessionMissingException("session was closed", snapshot);
if( ! session.isConnected() ) throwSessionDisconnectedException("session is disconnected", snapshot);

session.initializeCollection( collection, false );
}

private void throwSessionMissingException(String msg, CollectionSnapshot snapshot) {
String name = snapshot==null ?
"" : " of role: " + snapshot.getRole();
throw new SessionMissingException("failed to lazily initialize a collection" + name + " - " + msg);
}

private void throwSessionDisconnectedException(String msg, CollectionSnapshot snapshot) {
String name = snapshot==null ?
"" : " of role: " + snapshot.getRole();
throw new SessionDisconnectedException("failed to lazily initialize a collection" + name + " - " + msg);
}
}

Added two new subclasses of LazyInitializationException:
public class SessionDisconnectedException extends LazyInitializationException {
public SessionDisconnectedException(String msg) {
super(msg);
}
}

public class SessionMissingException extends LazyInitializationException {
public SessionMissingException(String msg) {
super(msg);
}
}


 All   Comments   Work Log   Change History   FishEye      Sort Order: Ascending order - Click to sort in descending order
Gavin King added a comment - 03/Aug/05 11:49 AM
-1