cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Cayenne Documentation > Obtaining DataContext
Date Sun, 20 Feb 2011 16:06:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2036/9/1/_/styles/combined.css?spaceKey=CAYDOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAYDOC/Obtaining+DataContext">Obtaining
DataContext</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~andrus">Andrus
Adamchik</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >DataContext context = (DataContext)
BaseContext.getThreadObjectContext();{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >{code}// we want to handle the
condition of no <span class="diff-changed-words">thread<span class="diff-added-chars"style="background-color:
#dfd;">-bound</span> context<span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">
exist</span>...</span> <br></td></tr>
            <tr><td class="diff-unchanged" >try { <br>    DataContext context
= (DataContext) BaseContext.getThreadObjectContext(); <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Depending on deployment environment and application needs, Cayenne can be
configured in a few different ways to make DataContext instances available. This is discussed
in detail in deployment chapter. But basically it comes down to getting a hold of an instance
of <tt>ServerRuntime</tt> and calling <tt>getContext()</tt> on it
as described below.</p>

<h3><a name="ObtainingDataContext-ObtainingaDataContextfromtheDIRuntime"></a>Obtaining
a DataContext from the DI Runtime</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">ServerRuntime runtime = ...
ObjectContext context = runtime.getContext()</pre>
</div></div>

<p>Note that every call to <tt>getContext()</tt> returns a new instance.
Also note that it returns <tt>ObjectContext</tt> (an interface implemented by
<tt>DataContext</tt>). If you need to use DataContext API not declared in ObjectContext,
you may use a cast:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">ServerRuntime runtime = ...
DataContext context = (DataContext) runtime.getContext()</pre>
</div></div>

<p>But if you don't, we recommend to stick with ObjectContext in your code, as it makes
the code more flexible and portable to the future versions of Cayenne.</p>

<h3><a name="ObtainingDataContext-RetrievingThreadBoundDataContext"></a>Retrieving
Thread-Bound DataContext</h3>

<p>An application can bind a context to a current execution thread (e.g. via <tt>CayenneFilter</tt>
or manually). Later on the code that needs DB access can retrieve this DataContext without
making any assumptions about the environment. This approach is universal and works in all
types of applications (web, standalone, etc.). Previously bound context can be retrieved by
calling <tt>BaseContext.getThreadObjectContext()</tt> static method. If no context
was bound to the current thread, this method throws IllegalStateException (same rules for
casting ObjectContext to DataContext as above apply) :</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// we are positive there is
DataContext in the current thread, and <span class="code-keyword">do</span> not
want
</span><span class="code-comment">// to handle possible exception...
</span>DataContext context = (DataContext) BaseContext.getThreadObjectContext();</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// we want to handle the condition
of no thread-bound context...
</span><span class="code-keyword">try</span> {
    DataContext context = (DataContext) BaseContext.getThreadObjectContext();
}
<span class="code-keyword">catch</span>(IllegalStateException ex) {
    <span class="code-comment">// handle failure
</span>    ....
}</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAYDOC/Obtaining+DataContext">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=10539&revisedVersion=9&originalVersion=8">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAYDOC/Obtaining+DataContext?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message