Re: [objectify-appengine] Objectify 4.0 and <threadsafe>true</threadsafe>

170 views
Skip to first unread message

Jeff Schnitzer

unread,
Nov 5, 2012, 2:47:37 AM11/5/12
to objectify...@googlegroups.com
On Mon, Nov 5, 2012 at 1:31 AM, Vidal B. <vidalb...@gmail.com> wrote:
What is the proper way (or ways) to use Objectify 4.0 with the <threadsafe>true</threadsafe> setting in an AppEngine app? The following is in the "Caching" Wiki page, but what else should we be aware of?
The session cache is not thread-safe. You should never share an Objectify instance between threads.

Just don't share an Objectify instance between multiple requests (eg, put it in a static context).  This is just normal programming in a servlet environment.  Very few parts of the GAE SDK are officially thread-safe, so this shouldn't be an unusual pattern.
 
 - Also, on a side-note: is there a way to temporarily disable the session cache for our own testing purposes - not involving ofy().clear()?

Not at present.  Because of the way @Load works, it's not a trivial change.

Jeff

Vidal B.

unread,
Nov 6, 2012, 9:19:42 PM11/6/12
to objectify...@googlegroups.com, je...@infohazard.org
Ok. Follow-up questions...

Given the following code:
Objectify objectifyInstance = ObjectifyService.factory().begin();

1) The javadoc for begin() indicates:

The default options are:

  • Do NOT begin a transaction.
  • Do NOT use a session cache.
  • DO use a global cache.
  • Use STRONG consistency.
  • Apply no deadline to calls.
But the wiki page says the session cache is enabled automatically. Is the javadoc just out-of-date?

2) And just to double-check, is the above code the proper way to start a new session (e.g. in a servlet's doPost() method) when running in threadsafe-mode?

3) When using multiple Objectify instances, is there anything (resource-wise) we need to deal with when an instance is about to be discarded (e.g. at the end of a doPost() method)?

Jeff Schnitzer

unread,
Nov 6, 2012, 9:28:06 PM11/6/12
to objectify...@googlegroups.com
On Tue, Nov 6, 2012 at 9:19 PM, Vidal B. <vidalb...@gmail.com> wrote:
Ok. Follow-up questions...

Given the following code:
Objectify objectifyInstance = ObjectifyService.factory().begin();

1) The javadoc for begin() indicates:

The default options are:

  • Do NOT begin a transaction.
  • Do NOT use a session cache.
  • DO use a global cache.
  • Use STRONG consistency.
  • Apply no deadline to calls.
But the wiki page says the session cache is enabled automatically. Is the javadoc just out-of-date?

Ooops, obsolete javadoc.  Fixed.

2) And just to double-check, is the above code the proper way to start a new session (e.g. in a servlet's doPost() method) when running in threadsafe-mode?

The proper way is to always call ofy(), the static method on ObjectifyService.  You will always get the right session.  You should not call begin() directly in your code.

All of the current documentation examples show the ofy() pattern.

3) When using multiple Objectify instances, is there anything (resource-wise) we need to deal with when an instance is about to be discarded (e.g. at the end of a doPost() method)?

No, there is nothing you need to do to close session.  But unless you really know what you're doing (say, running two transactions in parallel) you shouldn't even be asking this question - just use ofy() every time you want to access Objectify.  If you need to escape a transaction temporarily, use ofy().transactionless().load()...etc 

Jeff
BTW:  4.0 beta 1 should be in Maven Central tomorrow.  Just waiting for sonatype to approve the release.

Vidal B.

unread,
Nov 7, 2012, 12:40:47 PM11/7/12
to objectify...@googlegroups.com, je...@infohazard.org
Thanks for clearing that up.

I was peeking around in the Objectify source, and I understand how it works internally now...
static final ThreadLocal<Deque<Objectify>> STACK = new ThreadLocal<Deque<Objectify>>();

I have peace of mind.

Keep up the great work, Jeff. Objectify is awesome!

 
Reply all
Reply to author
Forward
0 new messages