You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Michael Gentry <mg...@masslight.net> on 2010/05/05 22:37:53 UTC

Session Replication

Someone on another project here asked me if Cayenne works with session
replication (with 1+ user DataContext objects in the HttpSession)
between application servers (such as JBoss).  They have a project
where they are running N instances of JBoss and they replicate/sync
the session data across the JBoss instances.  They are curious if the
DataContext + DataObjects will replicate correctly.  I've never
attempted to do so and am not sure.  Does anyone have experience with
this?

Thanks!

mrg

Re: Batch Insert

Posted by Marek Šabo <ms...@buk.cvut.cz>.
Hi Michael,

sorry for delay, I was kind of busy during weekend,
I played around a little bit and the newObject() approach seems to be
fine and is easily debuggable.

Thanks for sharing ideas, regards

--
Marek Šabo


On 05/08/2010 03:03 PM, Michael Gentry wrote:
> Hi Marek,
>
> I kind of lost track of this e-mail.  Sorry for the delay.
>
> I think the way you are doing it is fine -- especially since you are
> needing to set values.  A couple of optimizations you could make are
> to use a brand new DataContext for the batch inserrs and also break it
> into smaller chunks (like 100 or so) to keep the memory footprint
> leaner.
>
> However, your original question was about batch inserts.  You could look into:
>
> http://cayenne.apache.org/doc/api/index.html?org/apache/cayenne/query/InsertBatchQuery.html
>
> Personally, though, I'd just use the normal dataContext.newObject() approach.
>
> mrg
>
>
> On Wed, May 5, 2010 at 7:26 PM, Marek Šabo <ms...@buk.cvut.cz> wrote:
>   
>> Hi Michael,
>>
>> thanks, I do it this way as well and it's ok (~ 10k), but I was
>> wondering if there was a batch-insert. Just a thought - let's say:
>>
>> instead of:
>>
>>        SelectQuery sel2 = new SelectQuery(OrgIncome.class);
>>        List<OrgIncome> incomes = ctx.performQuery(sel2);
>>        for (OrgIncome oi : incomes) {
>>            Payment p = ctx.newObject(Payment.class);
>>            p.setAmount(oi.getAmount().doubleValue());
>>            p.setDate(oi.getDate());
>>            p.setNote(oi.getInfo());
>>            p.setVariableSymbol("" + oi.getVs());
>>        }
>>        ctx.commitChanges();
>>
>> you would use something like:
>>
>>        SelectQuery sel2 = new SelectQuery(OrgIncome.class);
>>        List<OrgIncome> incomes = ctx.performQuery(sel2);
>>        BatchInsertWrapper<OrgIncome> biw = new
>> BatchInsertWrapper<>(incomes);
>>        biw.commitTo(ctx);
>>
>> with some optimizations for mass inserts inside.
>>
>> Regards,
>>
>> On 05/05/2010 10:58 PM, Michael Gentry wrote:
>>     
>>> Hi Marek,
>>>
>>> I don't know how many objects you are talking about inserting, but
>>> I've done mass inserts (tens to thousands of objects) just using
>>> dataContext.commitChanges().
>>>
>>> mrg
>>>
>>>
>>> On Wed, May 5, 2010 at 4:55 PM, Marek Šabo <ms...@buk.cvut.cz> wrote:
>>>
>>>       
>>>> I've been searching for a method to persist list of DataObjects aka
>>>> batch insert or mass insert. Haven't found anything relevant so far. Is
>>>> there a support for this in cayenne or some optimized way to this?
>>>> (faster than iterating and single inserting)
>>>>
>>>>
>>>>         
>>>       
>>
>> --
>> Marek Šabo
>> Server Manager
>> Club SU CVUT Buben
>> Bubenečská Kolej (421)
>> XMPP: zeratul021@gmail.com
>>
>>
>>     
>   


Re: Batch Insert

Posted by Michael Gentry <mg...@masslight.net>.
Hi Marek,

I kind of lost track of this e-mail.  Sorry for the delay.

I think the way you are doing it is fine -- especially since you are
needing to set values.  A couple of optimizations you could make are
to use a brand new DataContext for the batch inserrs and also break it
into smaller chunks (like 100 or so) to keep the memory footprint
leaner.

However, your original question was about batch inserts.  You could look into:

http://cayenne.apache.org/doc/api/index.html?org/apache/cayenne/query/InsertBatchQuery.html

Personally, though, I'd just use the normal dataContext.newObject() approach.

mrg


On Wed, May 5, 2010 at 7:26 PM, Marek Šabo <ms...@buk.cvut.cz> wrote:
> Hi Michael,
>
> thanks, I do it this way as well and it's ok (~ 10k), but I was
> wondering if there was a batch-insert. Just a thought - let's say:
>
> instead of:
>
>        SelectQuery sel2 = new SelectQuery(OrgIncome.class);
>        List<OrgIncome> incomes = ctx.performQuery(sel2);
>        for (OrgIncome oi : incomes) {
>            Payment p = ctx.newObject(Payment.class);
>            p.setAmount(oi.getAmount().doubleValue());
>            p.setDate(oi.getDate());
>            p.setNote(oi.getInfo());
>            p.setVariableSymbol("" + oi.getVs());
>        }
>        ctx.commitChanges();
>
> you would use something like:
>
>        SelectQuery sel2 = new SelectQuery(OrgIncome.class);
>        List<OrgIncome> incomes = ctx.performQuery(sel2);
>        BatchInsertWrapper<OrgIncome> biw = new
> BatchInsertWrapper<>(incomes);
>        biw.commitTo(ctx);
>
> with some optimizations for mass inserts inside.
>
> Regards,
>
> On 05/05/2010 10:58 PM, Michael Gentry wrote:
>> Hi Marek,
>>
>> I don't know how many objects you are talking about inserting, but
>> I've done mass inserts (tens to thousands of objects) just using
>> dataContext.commitChanges().
>>
>> mrg
>>
>>
>> On Wed, May 5, 2010 at 4:55 PM, Marek Šabo <ms...@buk.cvut.cz> wrote:
>>
>>> I've been searching for a method to persist list of DataObjects aka
>>> batch insert or mass insert. Haven't found anything relevant so far. Is
>>> there a support for this in cayenne or some optimized way to this?
>>> (faster than iterating and single inserting)
>>>
>>>
>>
>
>
> --
> Marek Šabo
> Server Manager
> Club SU CVUT Buben
> Bubenečská Kolej (421)
> XMPP: zeratul021@gmail.com
>
>

Re: Batch Insert

Posted by Marek Šabo <ms...@buk.cvut.cz>.
Hi Michael,

thanks, I do it this way as well and it's ok (~ 10k), but I was
wondering if there was a batch-insert. Just a thought - let's say:

instead of:

        SelectQuery sel2 = new SelectQuery(OrgIncome.class);
        List<OrgIncome> incomes = ctx.performQuery(sel2);
        for (OrgIncome oi : incomes) {
            Payment p = ctx.newObject(Payment.class);
            p.setAmount(oi.getAmount().doubleValue());
            p.setDate(oi.getDate());
            p.setNote(oi.getInfo());
            p.setVariableSymbol("" + oi.getVs());
        }
        ctx.commitChanges();

you would use something like:

        SelectQuery sel2 = new SelectQuery(OrgIncome.class);
        List<OrgIncome> incomes = ctx.performQuery(sel2);
        BatchInsertWrapper<OrgIncome> biw = new
BatchInsertWrapper<>(incomes);
        biw.commitTo(ctx);

with some optimizations for mass inserts inside.

Regards,

On 05/05/2010 10:58 PM, Michael Gentry wrote:
> Hi Marek,
>
> I don't know how many objects you are talking about inserting, but
> I've done mass inserts (tens to thousands of objects) just using
> dataContext.commitChanges().
>
> mrg
>
>
> On Wed, May 5, 2010 at 4:55 PM, Marek Šabo <ms...@buk.cvut.cz> wrote:
>   
>> I've been searching for a method to persist list of DataObjects aka
>> batch insert or mass insert. Haven't found anything relevant so far. Is
>> there a support for this in cayenne or some optimized way to this?
>> (faster than iterating and single inserting)
>>
>>     
>   


-- 
Marek Šabo
Server Manager
Club SU CVUT Buben
Bubenečská Kolej (421)
XMPP: zeratul021@gmail.com


Re: Batch Insert

Posted by Michael Gentry <mg...@masslight.net>.
Hi Marek,

I don't know how many objects you are talking about inserting, but
I've done mass inserts (tens to thousands of objects) just using
dataContext.commitChanges().

mrg


On Wed, May 5, 2010 at 4:55 PM, Marek Šabo <ms...@buk.cvut.cz> wrote:
> I've been searching for a method to persist list of DataObjects aka
> batch insert or mass insert. Haven't found anything relevant so far. Is
> there a support for this in cayenne or some optimized way to this?
> (faster than iterating and single inserting)
>

Batch Insert

Posted by Marek Šabo <ms...@buk.cvut.cz>.
Hi,

I've been searching for a method to persist list of DataObjects aka
batch insert or mass insert. Haven't found anything relevant so far. Is
there a support for this in cayenne or some optimized way to this?
(faster than iterating and single inserting)

Regards,

-- 
Marek Šabo



Re: Session Replication

Posted by Andrus Adamchik <an...@objectstyle.org>.
On May 6, 2010, at 12:42 AM, Mike Kienenberger wrote:

> So it looks like the only trick is setting the channel on  
> unserialization.
> I don't remember the particulars of why I mark the context as
> temporary on unserialization.

Exactly. Serializing the entire DataContext is always the safest thing  
to do. It will reattach itself to the right channel automatically if  
the static Configuration singleton is used. If not, the user will have  
to do it manually. In 3.1 it is even more flexible per CAY-1425, as  
attaching to the channel is done via a ThreadLocal.

Andrus



Re: Session Replication

Posted by Mike Kienenberger <mk...@gmail.com>.
Ok.

Found some code.

Basically, I'm serializing a "DAO" class whose state is pretty much
just a DataContext.

        private void writeObject(java.io.ObjectOutputStream out)
throws IOException
        {
            out.writeObject(this.childDataContext);
        }
        private void readObject(java.io.ObjectInputStream in) throws
IOException, ClassNotFoundException
        {
            this.childDataContext = (DataContext)in.readObject();
            childDataContext.setChannel(super.threadDataContext().getParentDataDomain());
            this.childDataContext.setUserProperty("temporary", Boolean.TRUE);
        }

So it looks like the only trick is setting the channel on unserialization.
I don't remember the particulars of why I mark the context as
temporary on unserialization.
I don't see it used for anything other than debugging elsewhere.

On Wed, May 5, 2010 at 5:27 PM, Mike Kienenberger <mk...@gmail.com> wrote:
> I did find some tests written which explicitly serialized DataContext.
> Apparently I was serializing DataContext at one point, and had some
> issue where creating a data object, then deleting that data object
> without committing, and it was causing an issue with serialized
> DataContexts.
>
> On Wed, May 5, 2010 at 5:10 PM, Mike Kienenberger <mk...@gmail.com> wrote:
>> Hmm.   Unfortunately, I can't remember if my issue with that project
>> was that the DataContext wasn't being serialized or that it was and I
>> didn't want it to.  And this was 1.2, so the behavior may have changed
>> since then.
>>
>> But in either case, it seems pretty trivial.
>>
>> In the case of it being serialized, it already does what you want
>>
>> In the case of it not being serialized, you'd create a new DataContext
>> and dump in all of the serialized objects you had from the original
>> DataContext.
>>
>> I don't think there's any state that needs to be saved for a
>> DataContext other than the DataObjects it contains.   Maybe you'd also
>> need to save the initial fetched object snapshots if you were working
>> with optimistic locking.
>>
>> All I can say for sure is that I did a JSF project where we saved the
>> uncommitted DataObjects out as client-side data (serialized into the
>> generated html), then read them back in on the next request.   The
>> data would survive application restarts, so it should be equivalent to
>> moving it to another session or another server.
>>
>> I tried glancing through what project source code I had laying around,
>> but I couldn't find the code that dealt with the serialization -- too
>> many project modules to look through.
>>
>> On Wed, May 5, 2010 at 4:57 PM, Michael Gentry <mg...@masslight.net> wrote:
>>> My experience with Tapestry (4 & 5) serializing a DataObject to the
>>> HTML (which it does by default when you have a loop in a form) is that
>>> the deserializing of the DataObject (on the subsequent
>>> request/response) gives you a DataObject that is disassociated from
>>> the DataContext (not good).  Perhaps if the DataContext went along for
>>> the ride all would be well ...
>>>
>>> Thanks,
>>>
>>> mrg
>>>
>>>
>>> On Wed, May 5, 2010 at 4:53 PM, Mike Kienenberger <mk...@gmail.com> wrote:
>>>> No direct experience, but I know you can serialize DataObjects and I'm
>>>> 99% certain you can serialize DataContexts.   I think I did this in
>>>> one project in the past to preserve state between requests rather than
>>>> saving state in the sessions.
>>>>
>>>> On Wed, May 5, 2010 at 4:37 PM, Michael Gentry <mg...@masslight.net> wrote:
>>>>> Someone on another project here asked me if Cayenne works with session
>>>>> replication (with 1+ user DataContext objects in the HttpSession)
>>>>> between application servers (such as JBoss).  They have a project
>>>>> where they are running N instances of JBoss and they replicate/sync
>>>>> the session data across the JBoss instances.  They are curious if the
>>>>> DataContext + DataObjects will replicate correctly.  I've never
>>>>> attempted to do so and am not sure.  Does anyone have experience with
>>>>> this?
>>>>>
>>>>> Thanks!
>>>>>
>>>>> mrg
>>>>>
>>>>
>>>
>>
>

Re: Session Replication

Posted by Mike Kienenberger <mk...@gmail.com>.
I did find some tests written which explicitly serialized DataContext.
Apparently I was serializing DataContext at one point, and had some
issue where creating a data object, then deleting that data object
without committing, and it was causing an issue with serialized
DataContexts.

On Wed, May 5, 2010 at 5:10 PM, Mike Kienenberger <mk...@gmail.com> wrote:
> Hmm.   Unfortunately, I can't remember if my issue with that project
> was that the DataContext wasn't being serialized or that it was and I
> didn't want it to.  And this was 1.2, so the behavior may have changed
> since then.
>
> But in either case, it seems pretty trivial.
>
> In the case of it being serialized, it already does what you want
>
> In the case of it not being serialized, you'd create a new DataContext
> and dump in all of the serialized objects you had from the original
> DataContext.
>
> I don't think there's any state that needs to be saved for a
> DataContext other than the DataObjects it contains.   Maybe you'd also
> need to save the initial fetched object snapshots if you were working
> with optimistic locking.
>
> All I can say for sure is that I did a JSF project where we saved the
> uncommitted DataObjects out as client-side data (serialized into the
> generated html), then read them back in on the next request.   The
> data would survive application restarts, so it should be equivalent to
> moving it to another session or another server.
>
> I tried glancing through what project source code I had laying around,
> but I couldn't find the code that dealt with the serialization -- too
> many project modules to look through.
>
> On Wed, May 5, 2010 at 4:57 PM, Michael Gentry <mg...@masslight.net> wrote:
>> My experience with Tapestry (4 & 5) serializing a DataObject to the
>> HTML (which it does by default when you have a loop in a form) is that
>> the deserializing of the DataObject (on the subsequent
>> request/response) gives you a DataObject that is disassociated from
>> the DataContext (not good).  Perhaps if the DataContext went along for
>> the ride all would be well ...
>>
>> Thanks,
>>
>> mrg
>>
>>
>> On Wed, May 5, 2010 at 4:53 PM, Mike Kienenberger <mk...@gmail.com> wrote:
>>> No direct experience, but I know you can serialize DataObjects and I'm
>>> 99% certain you can serialize DataContexts.   I think I did this in
>>> one project in the past to preserve state between requests rather than
>>> saving state in the sessions.
>>>
>>> On Wed, May 5, 2010 at 4:37 PM, Michael Gentry <mg...@masslight.net> wrote:
>>>> Someone on another project here asked me if Cayenne works with session
>>>> replication (with 1+ user DataContext objects in the HttpSession)
>>>> between application servers (such as JBoss).  They have a project
>>>> where they are running N instances of JBoss and they replicate/sync
>>>> the session data across the JBoss instances.  They are curious if the
>>>> DataContext + DataObjects will replicate correctly.  I've never
>>>> attempted to do so and am not sure.  Does anyone have experience with
>>>> this?
>>>>
>>>> Thanks!
>>>>
>>>> mrg
>>>>
>>>
>>
>

Re: Session Replication

Posted by Mike Kienenberger <mk...@gmail.com>.
Hmm.   Unfortunately, I can't remember if my issue with that project
was that the DataContext wasn't being serialized or that it was and I
didn't want it to.  And this was 1.2, so the behavior may have changed
since then.

But in either case, it seems pretty trivial.

In the case of it being serialized, it already does what you want

In the case of it not being serialized, you'd create a new DataContext
and dump in all of the serialized objects you had from the original
DataContext.

I don't think there's any state that needs to be saved for a
DataContext other than the DataObjects it contains.   Maybe you'd also
need to save the initial fetched object snapshots if you were working
with optimistic locking.

All I can say for sure is that I did a JSF project where we saved the
uncommitted DataObjects out as client-side data (serialized into the
generated html), then read them back in on the next request.   The
data would survive application restarts, so it should be equivalent to
moving it to another session or another server.

I tried glancing through what project source code I had laying around,
but I couldn't find the code that dealt with the serialization -- too
many project modules to look through.

On Wed, May 5, 2010 at 4:57 PM, Michael Gentry <mg...@masslight.net> wrote:
> My experience with Tapestry (4 & 5) serializing a DataObject to the
> HTML (which it does by default when you have a loop in a form) is that
> the deserializing of the DataObject (on the subsequent
> request/response) gives you a DataObject that is disassociated from
> the DataContext (not good).  Perhaps if the DataContext went along for
> the ride all would be well ...
>
> Thanks,
>
> mrg
>
>
> On Wed, May 5, 2010 at 4:53 PM, Mike Kienenberger <mk...@gmail.com> wrote:
>> No direct experience, but I know you can serialize DataObjects and I'm
>> 99% certain you can serialize DataContexts.   I think I did this in
>> one project in the past to preserve state between requests rather than
>> saving state in the sessions.
>>
>> On Wed, May 5, 2010 at 4:37 PM, Michael Gentry <mg...@masslight.net> wrote:
>>> Someone on another project here asked me if Cayenne works with session
>>> replication (with 1+ user DataContext objects in the HttpSession)
>>> between application servers (such as JBoss).  They have a project
>>> where they are running N instances of JBoss and they replicate/sync
>>> the session data across the JBoss instances.  They are curious if the
>>> DataContext + DataObjects will replicate correctly.  I've never
>>> attempted to do so and am not sure.  Does anyone have experience with
>>> this?
>>>
>>> Thanks!
>>>
>>> mrg
>>>
>>
>

Re: Session Replication

Posted by Michael Gentry <mg...@masslight.net>.
My experience with Tapestry (4 & 5) serializing a DataObject to the
HTML (which it does by default when you have a loop in a form) is that
the deserializing of the DataObject (on the subsequent
request/response) gives you a DataObject that is disassociated from
the DataContext (not good).  Perhaps if the DataContext went along for
the ride all would be well ...

Thanks,

mrg


On Wed, May 5, 2010 at 4:53 PM, Mike Kienenberger <mk...@gmail.com> wrote:
> No direct experience, but I know you can serialize DataObjects and I'm
> 99% certain you can serialize DataContexts.   I think I did this in
> one project in the past to preserve state between requests rather than
> saving state in the sessions.
>
> On Wed, May 5, 2010 at 4:37 PM, Michael Gentry <mg...@masslight.net> wrote:
>> Someone on another project here asked me if Cayenne works with session
>> replication (with 1+ user DataContext objects in the HttpSession)
>> between application servers (such as JBoss).  They have a project
>> where they are running N instances of JBoss and they replicate/sync
>> the session data across the JBoss instances.  They are curious if the
>> DataContext + DataObjects will replicate correctly.  I've never
>> attempted to do so and am not sure.  Does anyone have experience with
>> this?
>>
>> Thanks!
>>
>> mrg
>>
>

Re: Session Replication

Posted by Mike Kienenberger <mk...@gmail.com>.
No direct experience, but I know you can serialize DataObjects and I'm
99% certain you can serialize DataContexts.   I think I did this in
one project in the past to preserve state between requests rather than
saving state in the sessions.

On Wed, May 5, 2010 at 4:37 PM, Michael Gentry <mg...@masslight.net> wrote:
> Someone on another project here asked me if Cayenne works with session
> replication (with 1+ user DataContext objects in the HttpSession)
> between application servers (such as JBoss).  They have a project
> where they are running N instances of JBoss and they replicate/sync
> the session data across the JBoss instances.  They are curious if the
> DataContext + DataObjects will replicate correctly.  I've never
> attempted to do so and am not sure.  Does anyone have experience with
> this?
>
> Thanks!
>
> mrg
>