These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: need PersistentList instead of PersistentBag for extraLazy?
PostPosted: Sat May 09, 2009 1:52 am 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
I have a big collection that I want to load as lazily as possible:

Code:
    @OneToMany(targetEntity = Friends.class, mappedBy = "firstUser")
    @Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @LazyCollection(LazyCollectionOption.EXTRA)
    @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    @OptimisticLock(excluded = true)   
    public List<Friends> getFriends() {
        return friends;
    }


I would like the following behavior to prevent fetching lots of data, and, more importantly, prevent insert of N Friends entities into 2nd level cache.
Code:
List<Friends> friends = parent.getFriends(); //select just the IDs of 'friends'
friends.size(); //no SELECTs -
friends.get(0); //select a single 'Friends' entitity


From what I can tell PersistentList combined with extraLazy will produce the desired behavior. However, Hibernate keeps returning PeristentBag and, after parent.getFriends() is called all N Friends entities end up in 2nd level cache. How do I get a PersistentList instead?

Using Hibernate Core 3.3.1 GA, Hibernate Annotations 3.4.0 GA, JBC 3.0.3 GA as 2nd level cache

thanks
-nikita


Top
 Profile  
 
 Post subject: Re: need PersistentList instead of PersistentBag for extraLazy?
PostPosted: Sat May 09, 2009 4:17 am 
Newbie

Joined: Thu Nov 15, 2007 3:17 am
Posts: 19
1. Take a look at @IndexColumn to switch from bag to list semantic.
2. Your Collection is marked as being cached via @Cache. Try to remove this to avoid caching.


Top
 Profile  
 
 Post subject: Re: need PersistentList instead of PersistentBag for extraLazy?
PostPosted: Sat May 09, 2009 2:20 pm 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
@IndexColumn seems to add an extra column to the schema - any way I can do without that?

RE: @Cache on the collection - I do want to cache the collection (create a Collection cache node); I just don't want to insert all collection members in Entity cache:
Imagine that a given User has 3000 Friends. User.friends Collection cache entry will essentially be a single node containing an array with 3000 Friends.IDs. I do want to cache that information. What I don't want is to eagerly insert 3000 Friend nodes into Entity cache.

Perhaps I should back up and be more specific:
- we have User entity and Friends entity. Friends is essentially a join table between two Users.
- we'd like to optimize returning Collection<User> that includes only those of user's friends who are currently online (which is often < 10% of all friends) and avoid storing in cache Friends entities pointing to offline users (i.e. the 90% case)
- we always have an in-memory Collection of userIDs of users currently online.

So, let's say Bob is our user with 3000 friends, of whom only 10 are online. Bob logs in and now has to get some info on online friends only and a count of all friends (those online and offline). Here's what I'm trying to get to:
Code:
User userBob = session.get(bobId); //caches one User entity

Collection<Friends> allFriends = userBob.getFriends();
//returns a proxy collection; creates a single Collection cache entry with 3000 IDs (need this cached to avoid repeat query)

//count all of bob's friends: issue sql like "select * from friends where firstUserId=bobId" - problem is this results in initialization (and caching) or 3000 Friends entities. need to avoid that
int totalFriends = allFriends.size();

//find out who's online in general
Collection<Long> onlineUserIDs = systemService.getOnlineUserIDs(); //another service that keeps tracks IDs of all users currently online

//now I want to filter allFriends using onlineUserIDs and only load and cache Friends and User entities for Bob's onlineFriends
Set<Friends> onlineFriends = session.createFilter(allFriends, " where this.secondUser.id in: " onlineUserIDs); //last arg is pseudo code

//now retrieve and cache needed info for the 10 online friends only
List<User> onlineFriendUsers = new ArrayList<User>(onlineFriends.size());
for(Friends f : onlineFriends)
{
User onlineUser = f.getSecondUser(); //fetch, cache, and initialize a single Friends entity; return User proxy
String userName = onlineUser.getUserName(); //fetch, cache, and initialize a single User entity
onlineFriendUsers.add(onlineUser);
}

//at the end of all this we should only have:
//11 Users in entity cache (bob + 10 online friends)
//1 user.friends entry in collection cache (with 3000 IDs)
//10 Friend entities in entity cache (one for each online friend of Bob)

return onlineFriendUsers;


The problem right now is that we end up with 3000 Friends entries in entity cache and that the perf suffers because of high memory consumption (under load) and because every time the above method is executed Hibernate hydrates 3000 Friends entities.




Schema:

Code:
Create Table: CREATE TABLE `friends` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `hibernateVersion` int(11) NOT NULL,
  `creationDate` datetime DEFAULT NULL,
  `firstUserId` bigint(20) NOT NULL,
  `secondUserId` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `firstUserId` (`firstUserId`,`secondUserId`),
  KEY `FKDC3B499515152FBC` (`firstUserId`),
  KEY `FKDC3B499528AE3E80` (`secondUserId`),
  CONSTRAINT `FKDC3B499515152FBC` FOREIGN KEY (`firstUserId`) REFERENCES `users` (`id`),
  CONSTRAINT `FKDC3B499528AE3E80` FOREIGN KEY (`secondUserId`) REFERENCES `users` (`id`)
)

Create Table: CREATE TABLE `users` (
  `id` bigint(20) NOT NULL,
  `hibernateVersion` int(11) NOT NULL,
  `userName` varchar(255) NOT NULL
)

    //User.friends collection:
    @OneToMany(targetEntity = Friends.class, mappedBy = "firstUser")
    @Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @LazyCollection(LazyCollectionOption.EXTRA)
    @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    @OptimisticLock(excluded = true)   
    public List<Friends> getFriends() {
        return friends;
    }



Top
 Profile  
 
 Post subject: Re: need PersistentList instead of PersistentBag for extraLazy?
PostPosted: Tue May 12, 2009 10:44 am 
Newbie

Joined: Tue May 12, 2009 10:36 am
Posts: 1
nikita

I was working configuring Hibernate Core 3.3.1 GA, Hibernate Annotations 3.4.0 GA, JBC 3.0.3 GA as 2nd level cache in Glassfish using TreeCache

But I was getting configuration file error

[#|2009-05-12T12:03:39.408+0000|SEVERE|sun-appserver9.1|org.jboss.cache.config.parsing.RootElementBuilder|_ThreadID=19;_ThreadName=Thread-89;_RequestID=d00e6d37-74ea-47af-b372-100bb87a735d;|Configuration warning: cvc-elt.1: Cannot find the declaration of element 'server'.|#]

[#|2009-05-12T12:03:39.408+0000|SEVERE|sun-appserver9.1|org.jboss.cache.config.parsing.RootElementBuilder|_ThreadID=19;_ThreadName=Thread-89;_RequestID=d00e6d37-74ea-47af-b372-100bb87a735d;|org.jboss.cache.config.ConfigurationException: Incorrect configuration file. Use '-Djbosscache.config.validate=false' to disable validation.|#]


Can you guide me with your copy of xml configuration .

Mine is treecache.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<server>
    <mbean code="org.jboss.cache.jmx.CacheJmxWrapper" name="jboss.cache:service=TreeCache">
        <depends>jboss:service=Naming</depends>
        <depends>jboss:service=TransactionManager</depends>
        <attribute name="TransactionManagerLookupClass">org.jboss.cache.transaction.GenericTransactionManagerLookup</attribute>
        <attribute name="NodeLockingScheme">PESSIMISTIC</attribute>
        <attribute name="IsolationLevel">REPEATABLE_READ</attribute>
        <attribute name="LockParentForChildInsertRemove">true</attribute>
        <attribute name="CacheMode">INVALIDATION_ASYNC</attribute>
        <attribute name="ClusterName">OIX-Cluster</attribute>
        <attribute name="ClusterConfig">
            <config>
                <UDP mcast_addr="224.0.0.251" mcast_port="4886" ip_ttl="64" ip_mcast="true" mcast_send_buf_size="150000"
                    mcast_recv_buf_size="80000" ucast_send_buf_size="150000" ucast_recv_buf_size="80000" loopback="false"
                    enable_bundling="true" />
                <PING timeout="2000" num_initial_members="3" />
                <MERGE2 min_interval="10000" max_interval="20000" />
                <FD shun="true" />
                <FD_SOCK />
                <VERIFY_SUSPECT timeout="1500" />
                <pbcast.NAKACK gc_lag="50" retransmit_timeout="600,1200,2400,4800" />
                <UNICAST timeout="600,1200,2400,4800" />
                <pbcast.STABLE desired_avg_gossip="400000" />
                <FC max_credits="2000000" min_threshold="0.10" />
                <FRAG2 frag_size="8192" />
                <pbcast.GMS join_timeout="5000" shun="true" print_local_addr="true" />
                <pbcast.STATE_TRANSFER />
            </config>
        </attribute>
        <attribute name="StateRetrievalTimeout">20000</attribute>
        <attribute name="SyncReplTimeout">20000</attribute>
        <attribute name="LockAcquisitionTimeout">15000</attribute>
        <attribute name="ShutdownHookBehavior">DEFAULT</attribute>
        <attribute name="UseLazyDeserialization">true</attribute>
    </mbean>
</server>


Thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.