Search the web
Sign In
New User? Sign Up
SimpleORM
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Hear how Yahoo! Groups has changed the lives of others. Take me there.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
The Big Split -- SimpleOrm.records and SimpleOrm.database.   Message List  
Reply | Forward Message #1561 of 1851 |
Thread Local Session

Hmm.

The motivations for attaching the session to a thread local were:-

1. It avoids having to pass a connection variable everywhere. In a
database application virtually everything needs to interact with the
database so the extra parameter is a pain.

2. More importantly it makes the threading model clear -- one thread
per connection. Particularly in the early days of Orms people did
bad things with sharing Jdbc connections between threads.

3. I makes the code slightly shorter.

But that said, it is unusual, and unusual is bad. And it does
conflict with our new idea of detached data sets.

So... if you want to we could separate the code like you suggest.

myDetachedDataset.findOrCreate(...); // this can happen without any session

Session ses = SSession.open(datasource);
ses.begin();
ses.attachDataSet(ds);
List<Record> res = ses.findOrcreate(...);
ses.flush();
SDataSet newDs = ses.detachDataSet();
ses.commit();
ses.close();
...

To do this SRecordFinder and SSession should be merged, and it is
SSession that should implement SDataSet.

If the ses.flush() is not done, I suppose newDs would just end up
with dirty records, and the implicit flush in the ses.commit() would
have nothing to do as the records would already have been moved.


I would still like to keep the option of a thread local. Perhaps
using the following style. It is probably SDataSets that can be
optionally connected to the thread locals rather than Sessions.

Session ses2 = SSession.open(datasource);

ses2.setThreadLocal(); // error if there is already a thread local dataset
...
Session ses3=SDataSet.getThreadLocal();
ses3.findOrCreate(...)
...
ses3.close(); // and remove from thread local

(or ses3.clearThreadLocal() to just disconnect without closing
everything. This is in SDataSet, close is in SSession.)


I would also like to add some thread traps. Eg.
volatile private trap=false
doFindOrCreate {
if ( trap ) throw threading error
trap = true
....
finally trap = false
}

It feels like we are getting to the end of the big changes. I
certainly hope so.

Anthony



At 10:35 PM 3/07/2008, Franck Routier wrote:

>Anthony,
>
>I was wondering, why must have so many static methods in SSession, that
>most of the time end up finding the current scon in threadlocal, instead
>of returning a SSession instance on open(), and passing around the
>pointer as needed ?
>
>(I must admit I am not that comfortable with multi-threading issues, so
>what I am saying might be stupid)
>
>Wouldn't:
>
>SSession ses = SSession.open(datasource);
>
>ses.begin();
>ses.attachDataSet(ds);
>List<Record> res = ses.findOrcreate(...);
>ses.flush();
>SDataSet newDs = ses.detachDataSet();
>ses.commit();
>
>SSession.closeSession();
>
>be better ? So the factory+threadLocal part of SSession would be clearly
>identified (public static methods open() and closeSession()), every
>other public method beeing instance methods. This would avoid having to
>get the current session from threadLocal every now and then in the code,
>and would make clear SSession really has two distinct purposes.
>
>As I already said, I am not sure at all about what I'm stating, so your
>opinion on this really matters :)
>
>Franck
>

Dr Anthony Berglas, anthony@... Mobile: +61 4 4838 8874
Just because it is possible to push twigs along the ground with ones nose
does not necessarily mean that is the best way to collect firewood.




Fri Jul 4, 2008 2:11 am

aberglas
Offline Offline
Send Email Send Email

Forward
Message #1561 of 1851 |
Expand Messages Author Sort by Date

Hello Franck, I think that as part of 3.0 we should consider splitting SimpleOrm into two pieces, each in their own Jars. simpleorm.records that contains...
Anthony Berglas
aberglas
Offline Send Email
Jun 30, 2008
5:57 am

Hello Frank, Moving SQuery out is possible. One would have to store the built query as a structure rather than a query string, and then translate to string in...
Anthony & Melissa Ber...
berglas@...
Send Email
Jun 30, 2008
10:45 am

Anthony, maybe we should consider introducing SDataSet (or SDataSetManager ?) right now. First it would be equivalent to SConnection's transaction cache and ...
Franck Routier
routier_franck
Offline Send Email
Jun 30, 2008
2:24 pm

Hello Franck, Hmm. The design gets ever more ambitious. I am concerned that if we take on too much we will not finish. But it would be good to get this...
Anthony Berglas
aberglas
Offline Send Email
Jul 1, 2008
11:18 am

Hi Anthony, that seems fine to me, except for one point that is not that clear :) shouldn't "myDS.create(new Employee().set(Employee.ID, "123").set.....);"...
Franck Routier
routier_franck
Offline Send Email
Jul 1, 2008
2:46 pm

Anthony, I've done some work, namely steps 1 to 6, execpt for the 'flush before query part'. Lot of thinks are now public, including some fields. Bad. Also...
Franck Routier
routier_franck
Offline Send Email
Jul 1, 2008
7:59 pm

Hello Franck, Wow, you have been busy. I agree that we need to be careful about our vocabulary, I had been a bit sloppy. SCon."attach/detach" had referred to ...
Anthony Berglas
aberglas
Offline Send Email
Jul 2, 2008
10:18 am

Hello Anthony, I agree with everything you say here. Regarding naming, maybe we should consider JPA terminology ? Then Session would be EntityManager. And...
Franck Routier
routier_franck
Offline Send Email
Jul 2, 2008
10:52 am

Hello Franck, You've been busy again. ... I'm not sure that joinTranction a good description for SSession.open. Maybe ...
Anthony Berglas
aberglas
Offline Send Email
Jul 3, 2008
5:11 am

Hi, ok, I really like SSession.newSessionAndAssociateWithCurrentThreadAndOpenJdbcConnection(), which really says what it does..... :) I agree with the fact...
Franck Routier
routier_franck
Offline Send Email
Jul 3, 2008
7:50 am

Anthony, I was wondering, why must have so many static methods in SSession, that most of the time end up finding the current scon in threadlocal, instead of...
Franck Routier
routier_franck
Offline Send Email
Jul 3, 2008
12:35 pm

Hmm. The motivations for attaching the session to a thread local were:- 1. It avoids having to pass a connection variable everywhere. In a database...
Anthony Berglas
aberglas
Offline Send Email
Jul 4, 2008
2:11 am

Hello Franck To keep it simple, the semantics of attaching a data set to a session could be exactly the same as attaching each record in it, one by one. As ...
Anthony Berglas
aberglas
Offline Send Email
Jul 4, 2008
12:47 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help