Re: [SimpleORM] if(!session.hasBe gun()) begin() … if (!wasBegun) commit
Franck Routier <[email protected]> Wed, 07 Mar 2012 14:09:01 +0100
| Newsgroups | gmane.comp.java.orm.simpleorm |
|---|---|
| Message-ID | <[email protected]> |
--------------070707010401060305020105
Content-Type: text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding: 8bit
Hi,
Le 06/03/2012 23:16, John Abraham a écrit :
>
> But some data does get changed, and there is always a chance that some
> data might not get queried until much later in the simulation. So I
> think I need to keep a connection open, just commit it after the
> initial burst of queries, then <begin
. commit> for any later queries
> that occur.
>
Why do you want to keep the connection open ? If this is for performance
reasons, you should probably consider using a connection pool (like
bonecp, or C3P0).
> What's the best practice for begin.. commit wrapping like this?
Well, standard usage is like following :
SSessionJdbc ses = SSessionJdbc.getThreadLocalSession();
try {
ses.begin();
... do you db work here
ses.commit();
}
finally {
ses.close();
}
>
> I was thinking of wrapping a lot of my stuff in
>
> boolean wasBegun = true;
> if (!session.hasBegun()) {
> session.begin();
> wasBegun=false;
> }
>
>
do my query and stuff
.
>
> if (!wasBegun) session.commit();
>
>
> This way I can use a big begin() commit() wrap for the all the initial
> queries, then rely on smaller begin() commit() wraps for the stuff
> that occurs randomly later on.
>
> What do you think of this plan? Is there a better system?
Do you need to handle one big transaction ? Simpleorm makes its best
effort to assure things are coherent, using the optimistic lock
approach. If one big transactin is not a real need for you, you should
probably have something like :
SDataSet staticDataDs = new SDataSet();
SSessionJdbc ses = SSessionJdbc.getThreadLocalSession();
try {
ses.begin(staticDataDs);
... big initial queries here
ses.commitAndDetachDataSet();
}
finally {
ses.close();
}
Then later on, when you happen to need to access the database,
SSessionJdbc ses = SSessionJdbc.getThreadLocalSession();
try {
ses.begin();
... other db work goes here
ses.commitAndDetachDataSet();
}
finally {
ses.close();
}
Does it make sense ?
Regards,
Franck
--------------070707010401060305020105
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: 8bit
<head>
<style type="text/css">
<!--
/* start of attachment style */
.ygrp-photo-title{
clear: both;
font-size: smaller;
height: 15px;
overflow: hidden;
text-align: center;
width: 75px;
}
div.ygrp-photo{
background-position: center;
background-repeat: no-repeat;
background-color: white;
border: 1px solid black;
height: 62px;
width: 62px;
}
div.photo-title
a,
div.photo-title a:active,
div.photo-title a:hover,
div.photo-title a:visited {
text-decoration: none;
}
div.attach-table div.attach-row {
clear: both;
}
div.attach-table div.attach-row div {
float: left;
/* margin: 2px;*/
}
p {
clear: both;
padding: 15px 0 3px 0;
overflow: hidden;
}
div.ygrp-file {
width: 30px;
valign: middle;
}
div.attach-table div.attach-row div div a {
text-decoration: none;
}
div.attach-table div.attach-row div div span {
font-weight: normal;
}
div.ygrp-file-title {
font-weight: bold;
}
/* end of attachment style */
-->
</style>
</head>
<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Hi,<br>
<br>
Le 06/03/2012 23:16, John Abraham a écrit :
<blockquote
cite="mid:[email protected]"
type="cite">
<span style="display:none"> </span>
<div id="ygrp-text"><br>
<div>But some data does get changed, and there is always a
chance that some data might not get queried until much
later in the simulation. So I think I need to keep a
connection open, just commit it after the initial burst of
queries, then <begin
. commit> for any later queries
that occur.</div>
<div><br>
</div>
</div>
</div>
</div>
</blockquote>
Why do you want to keep the connection open ? If this is for
performance reasons, you should probably consider using a connection
pool (like bonecp, or C3P0).<br>
<br>
<blockquote
cite="mid:[email protected]"
type="cite">
<div id="ygrp-mlmsg" style="position: relative;">
<div id="ygrp-msg" style="z-index: 1;">
<div id="ygrp-text">
<div>What's the best practice for begin.. commit wrapping
like this?</div>
</div>
</div>
</div>
</blockquote>
Well, standard usage is like following :<br>
<br>
SSessionJdbc ses = SSessionJdbc.getThreadLocalSession();<br>
try {<br>
ses.begin();<br>
<br>
... do you db work here<br>
<br>
ses.commit();<br>
}<br>
finally {<br>
ses.close();<br>
}<br>
<blockquote
cite="mid:[email protected]"
type="cite">
<div id="ygrp-mlmsg" style="position: relative;">
<div id="ygrp-msg" style="z-index: 1;">
<div id="ygrp-text">
<div><br>
</div>
<div>I was thinking of wrapping a lot of my stuff in</div>
<div><br>
</div>
<div><font class="Apple-style-span" face="'Courier New'">boolean
wasBegun = true;</font></div>
<div><font class="Apple-style-span" face="'Courier New'">if
(!session.hasBegun()) {</font></div>
<div><font class="Apple-style-span" face="'Courier New'">
session.begin();</font></div>
<div><font class="Apple-style-span" face="'Courier New'">
wasBegun=false;</font></div>
<div><font class="Apple-style-span" face="'Courier New'">}</font></div>
<div><font class="Apple-style-span" face="'Courier New'"><br>
</font></div>
<div><font class="Apple-style-span" face="'Courier New'">
do my query and stuff
.</font></div>
<div><font class="Apple-style-span" face="'Courier New'"><br>
</font></div>
<div><font class="Apple-style-span" face="'Courier New'">if
(!wasBegun) session.commit();</font></div>
<div><br>
</div>
<div><br>
</div>
<div>This way I can use a big begin() commit() wrap for the
all the initial queries, then rely on smaller begin()
commit() wraps for the stuff that occurs randomly later
on.</div>
<div><br>
</div>
<div>What do you think of this plan? Is there a better
system?</div>
</div>
</div>
</div>
</blockquote>
Do you need to handle one big transaction ? Simpleorm makes its best
effort to assure things are coherent, using the optimistic lock
approach. If one big transactin is not a real need for you, you
should probably have something like :<br>
<br>
SDataSet staticDataDs = new SDataSet();<br>
SSessionJdbc ses = SSessionJdbc.getThreadLocalSession();<br>
try {<br>
ses.begin(staticDataDs);<br>
... big initial queries here<br>
ses.commitAndDetachDataSet();<br>
}<br>
finally {<br>
ses.close();<br>
}<br>
<br>
Then later on, when you happen to need to access the database, <br>
<br>
SSessionJdbc ses = SSessionJdbc.getThreadLocalSession();<br>
try {<br>
ses.begin();<br>
... other db work goes here<br>
ses.commitAndDetachDataSet();<br>
}<br>
finally {<br>
ses.close();<br>
}<br>
<br>
<br>
Does it make sense ?<br>
<br>
Regards,<br>
Franck<br>
<!-- |**|begin egp html banner|**| -->
<br>
<br>
<!-- |**|end egp html banner|**| -->
<div width="1" style="color: white; clear: both;"/>__._,_.___</div>
<!-- Start Recommendations -->
<!-- End Recommendations -->
<!-- |**|begin egp html banner|**| -->
<img src="http://geo.yahoo.com/serv?s=97476590/grpId=7360226/grpspId=1705006905/msgId=2046/stime=1331125777" width="1" height="1"> <br>
<!-- |**|end egp html banner|**| -->
<!-- |**|begin egp html banner|**| -->
<br>
<div style="font-family: verdana; font-size: 77%; border-top: 1px solid #666; padding: 5px 0;" >
Your email settings: Individual Email|Traditional <br>
<a href="http://groups.yahoo.com/group/SimpleORM/join;_ylc=X3oDMTJmbTI5ajRuBF9TAzk3NDc2NTkwBGdycElkAzczNjAyMjYEZ3Jwc3BJZAMxNzA1MDA2OTA1BHNlYwNmdHIEc2xrA3N0bmdzBHN0aW1lAzEzMzExMjU3Nzc-">Change settings via the Web</a> (Yahoo! ID required) <br>
Change settings via email: <a href="mailto:[email protected]?subject=Email Delivery: Digest">Switch delivery to Daily Digest</a> | <a href = "mailto:[email protected]?subject=Change Delivery Format: Fully Featured">Switch to Fully Featured</a> <br>
<a href="http://groups.yahoo.com/group/SimpleORM;_ylc=X3oDMTJkdHMyODkwBF9TAzk3NDc2NTkwBGdycElkAzczNjAyMjYEZ3Jwc3BJZAMxNzA1MDA2OTA1BHNlYwNmdHIEc2xrA2hwZgRzdGltZQMxMzMxMTI1Nzc3">
Visit Your Group
</a> |
<a href="http://docs.yahoo.com/info/terms/">
Yahoo! Groups Terms of Use
</a> |
<a href="mailto:[email protected]?subject=Unsubscribe">
Unsubscribe
</a>
<br>
</div>
<br>
<!-- |**|end egp html banner|**| -->
<div style="color: white; clear: both;"/>__,_._,___</div>
</body>
</html>
--------------070707010401060305020105--