Re: [SimpleORM] if(!session.hasBegun ()) begin() … if (!wasBegun) co mmit
John Abraham <[email protected]> Wed, 7 Mar 2012 08:29:49 -0700
| Newsgroups | gmane.comp.java.orm.simpleorm |
|---|---|
| Message-ID | <[email protected]> |
--Apple-Mail=_CCCE357C-B909-415C-8F84-39549CDBFEE9
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=windows-1252
Thank you Franck for your ideas.
I assumed there was a lot of overhead in reattaching a dataset. I don't kn=
ow why I assumed that, looking at the code it seems it does nothing except =
some precondition checking and setting pointers. I also assumed reattach=
ing the SDataSet would start a new transaction in the database, but again, =
it seems to do nothing of the sort.
=09
I'm going to try doing a commitAndDetachDataSet() followed immediately by r=
eattaching the dataset. This may release the transaction lock on the datab=
ase until the next query (the database would be IDLE instead of IDLE IN TRA=
NSACTION). If I do this frequently enough it might work fine. I might eve=
n call this combination flushWithCommit().
I use SSessionJdbc.getThreadLocalSession().find() in a lot of places in the=
code to get objects by their primary key. If necessary I could replace al=
l of these with a call to a utility routine somewhere that does
object =3D mySDataSet.find()
if (object =3D=3D null) {
ses.attach(mySDataSet)
ses.mustFind()
}
=85 set up some timing thing so that if there aren't any hits to the databa=
se in 5 minutes or so commmitAndDetach the dataset again, only the commitAn=
dDetach can't happen in a separate thread because SimpleORM isn't thread sa=
fe so I'd have to poll a timer now and again in my main thread.
This seems like a lot of work and complexity when all I want to do is write=
the changes to the database to free up the transaction processing on the d=
atabase side.
> Why do you want to keep the connection open ?
If I have to write a utility routine to manage the detaching and attaching,=
it could also close the connection too after longer inactivity, and open a=
new one when the app needs it. But there doesn't seem to be a problem wit=
h having connections open, as long as they aren't in a transaction. Lookin=
g at my databases logs I see multiple connections from PGAdminIII and Quant=
um GIS lying open for days because people haven't closed the apps on their =
machines.
--
John=20
On 2012-03-07, at 6:09 AM, Franck Routier wrote:
> Hi,
>=20
> Le 06/03/2012 23:16, John Abraham a =E9crit :
>>=20
>>=20
>> But some data does get changed, and there is always a chance that some d=
ata 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=85. commit> for any later queries that occur.
>>=20
>=20
>> What's the best practice for begin.. commit wrapping like this?
> Well, standard usage is like following :
>=20
> SSessionJdbc ses =3D SSessionJdbc.getThreadLocalSession();
> try {
> ses.begin();
>=20
> ... do you db work here
>=20
> ses.commit();
> }
> finally {
> ses.close();
> }
>>=20
>> I was thinking of wrapping a lot of my stuff in
>>=20
>> boolean wasBegun =3D true;
>> if (!session.hasBegun()) {
>> session.begin();
>> wasBegun=3Dfalse;
>> }
>>=20
>> =85 do my query and stuff=85.
>>=20
>> if (!wasBegun) session.commit();
>>=20
>>=20
>> This way I can use a big begin() commit() wrap for the all the initial q=
ueries, then rely on smaller begin() commit() wraps for the stuff that occu=
rs randomly later on.
>>=20
>> What do you think of this plan? Is there a better system?
> Do you need to handle one big transaction ? Simpleorm makes its best effo=
rt to assure things are coherent, using the optimistic lock approach. If on=
e big transactin is not a real need for you, you should probably have somet=
hing like :
>=20
> SDataSet staticDataDs =3D new SDataSet();
> SSessionJdbc ses =3D SSessionJdbc.getThreadLocalSession();
> try {
> ses.begin(staticDataDs);
> ... big initial queries here
> ses.commitAndDetachDataSet();
> }
> finally {
> ses.close();
> }
>=20
> Then later on, when you happen to need to access the database,=20
>=20
> SSessionJdbc ses =3D SSessionJdbc.getThreadLocalSession();
> try {
> ses.begin();
> ... other db work goes here
> ses.commitAndDetachDataSet();
> }
> finally {
> ses.close();
> }
>=20
>=20
> Does it make sense ?
>=20
> Regards,
> Franck
--Apple-Mail=_CCCE357C-B909-415C-8F84-39549CDBFEE9
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable
<head>
<style type=3D"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=20
a,
div.photo-title a:active,
div.photo-title a:hover,
div.photo-title a:visited {
text-decoration: none;=20
}
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></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode:=
space; -webkit-line-break: after-white-space; ">
<div>Thank you Franck for your ideas.</div><div><br></div><div>I assumed th=
ere was a lot of overhead in reattaching a dataset. I don't know why =
I assumed that, looking at the code it seems it does nothing except some pr=
econdition checking and setting pointers. I also assumed =
reattaching the SDataSet would start a new transaction in the database, but=
again, it seems to do nothing of the sort.</div><div><span class=3D"Apple-=
tab-span" style=3D"white-space:pre"> </span></div><div>I'm going to try doi=
ng a commitAndDetachDataSet() followed immediately by reattaching the datas=
et. This may release the transaction lock on the database until the n=
ext query (the database would be IDLE instead of IDLE IN TRANSACTION). &nbs=
p;If I do this frequently enough it might work fine. I might even cal=
l this combination flushWithCommit().</div><div><br></div><div>I use SSessi=
onJdbc.getThreadLocalSession().find() in a lot of places in the code to get=
objects by their primary key. If necessary I could replace all of th=
ese with a call to a utility routine somewhere that does</div><div><br></di=
v><div><font class=3D"Apple-style-span" face=3D"Courier" style=3D"font-size=
: 11px;">object =3D mySDataSet.find()</font></div><div><font class=3D=
"Apple-style-span" face=3D"Courier" style=3D"font-size: 11px;">if (object =
=3D=3D null) {</font></div><div><font class=3D"Apple-style-span" face=3D"Co=
urier" style=3D"font-size: 11px;"><span class=3D"Apple-tab-span" style=3D"w=
hite-space:pre"> </span>ses.attach(mySDataSet)</font></div><div><font class=
=3D"Apple-style-span" face=3D"Courier" style=3D"font-size: 11px;"><span cla=
ss=3D"Apple-tab-span" style=3D"white-space:pre"> </span>ses.mustFind()</fon=
t></div><div><font class=3D"Apple-style-span" face=3D"Courier" style=3D"fon=
t-size: 11px;">}</font></div><div><font class=3D"Apple-style-span" face=3D"=
Courier" style=3D"font-size: 11px;">=85 set up some timing thing so that if=
there aren't any hits to the database in 5 minutes or so commmitAndDetach =
the dataset again, only the commitAndDetach can't happen in a separate thre=
ad because SimpleORM isn't thread safe so I'd have to poll a timer now and =
again in my main thread.</font></div><div><br></div><div>This seems like a =
lot of work and complexity when all I want to do is write the changes to th=
e database to free up the transaction processing on the database side.</div=
><div><br></div><div></div><blockquote type=3D"cite"><div>Why do you want t=
o keep the connection open ?<br></div></blockquote><div><br></div>If I have=
to write a utility routine to manage the detaching and attaching, it could=
also close the connection too after longer inactivity, and open a new one =
when the app needs it. But there doesn't seem to be a problem with ha=
ving connections open, as long as they aren't in a transaction. Looki=
ng at my databases logs I see multiple connections from PGAdminIII and Quan=
tum GIS lying open for days because people haven't closed the apps on their=
machines.<br><div><br></div><div>--</div><div>John </div><div><br></d=
iv><div><br></div><br><div><div>On 2012-03-07, at 6:09 AM, Franck Routier w=
rote:</div><br class=3D"Apple-interchange-newline"><blockquote type=3D"cite=
">
=20=20
<meta content=3D"text/html; charset=3Dwindows-1252" http-equiv=3D"Conte=
nt-Type">
=20=20
<div bgcolor=3D"#FFFFFF" text=3D"#000000">
Hi,<br>
<br>
Le 06/03/2012 23:16, John Abraham a =E9crit :
<blockquote cite=3D"mid:8FFD3646-0320-4AEB-98C2-4FA814B1CA6C@hbaspecto.=
com" type=3D"cite">
<span style=3D"display:none"> </span>
=20=20=20=20=20=20
<div id=3D"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=85. commit> for any later queries
that occur.</div>
<div><br>
</div>
</div>
</div>
</div>
</blockquote><br>
<blockquote cite=3D"mid:8FFD3646-0320-4AEB-98C2-4FA814B1CA6C@hbaspecto.=
com" type=3D"cite">
<div id=3D"ygrp-mlmsg" style=3D"position: relative;">
<div id=3D"ygrp-msg" style=3D"z-index: 1;">
<div id=3D"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 =3D 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=3D"mid:8FFD3646-0320-4AEB-98C2-4FA814B1CA6C@hbaspecto.=
com" type=3D"cite">
<div id=3D"ygrp-mlmsg" style=3D"position: relative;">
<div id=3D"ygrp-msg" style=3D"z-index: 1;">
<div id=3D"ygrp-text">
<div><br>
</div>
<div>I was thinking of wrapping a lot of my stuff in</div>
<div><br>
</div>
<div><font class=3D"Apple-style-span" face=3D"'Courier New'">bo=
olean
wasBegun =3D true;</font></div>
<div><font class=3D"Apple-style-span" face=3D"'Courier New'">if
(!session.hasBegun()) {</font></div>
<div><font class=3D"Apple-style-span" face=3D"'Courier New'">&n=
bsp;
session.begin();</font></div>
<div><font class=3D"Apple-style-span" face=3D"'Courier New'">&n=
bsp;
wasBegun=3Dfalse;</font></div>
<div><font class=3D"Apple-style-span" face=3D"'Courier New'">}<=
/font></div>
<div><font class=3D"Apple-style-span" face=3D"'Courier New'"><b=
r>
</font></div>
<div><font class=3D"Apple-style-span" face=3D"'Courier New'">=
=85
do my query and stuff=85.</font></div>
<div><font class=3D"Apple-style-span" face=3D"'Courier New'"><b=
r>
</font></div>
<div><font class=3D"Apple-style-span" face=3D"'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 =3D new SDataSet();<br>
SSessionJdbc ses =3D 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 =3D 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>
</div>
</blockquote></div><br>
<!-- |**|begin egp html banner|**| -->
<br>
=20=20=20=20
=20=20=20=20
<br>
<!-- |**|end egp html banner|**| -->
<div width=3D"1" style=3D"color: white; clear: both;"/>__._,_.___</div>
<!-- Start Recommendations -->
<!-- End Recommendations -->
<!-- |**|begin egp html banner|**| -->
<img src=3D"http://geo.yahoo.com/serv?s=3D97476590/grpId=3D7360226/grpspI=
d=3D1705006905/msgId=3D2047/stime=3D1331134202" width=3D"1" height=3D"1"> <=
br>
<!-- |**|end egp html banner|**| -->
=20=20=20=20
<!-- |**|begin egp html banner|**| -->
<br>
<div style=3D"font-family: verdana; font-size: 77%; border-top: 1px s=
olid #666; padding: 5px 0;" >
Your email settings: Individual Email|Traditional <br>
<a href=3D"http://groups.yahoo.com/group/SimpleORM/join;_ylc=3DX3oDMT=
JmNDhoZzVjBF9TAzk3NDc2NTkwBGdycElkAzczNjAyMjYEZ3Jwc3BJZAMxNzA1MDA2OTA1BHNlY=
wNmdHIEc2xrA3N0bmdzBHN0aW1lAzEzMzExMzQyMDI-">Change settings via the Web</a=
> (Yahoo! ID required) <br>
Change settings via email: <a href=3D"mailto:SimpleORM-digest@yahoogr=
oups.com?subject=3DEmail Delivery: Digest">Switch delivery to Daily Digest<=
/a> | <a href =3D "mailto:[email protected]?subject=3D=
Change Delivery Format: Fully Featured">Switch to Fully Featured</a> <br>
<a href=3D"http://groups.yahoo.com/group/SimpleORM;_ylc=3DX3oDMT=
JkYWFoNm9qBF9TAzk3NDc2NTkwBGdycElkAzczNjAyMjYEZ3Jwc3BJZAMxNzA1MDA2OTA1BHNlY=
wNmdHIEc2xrA2hwZgRzdGltZQMxMzMxMTM0MjAy">
Visit Your Group=20
</a> |
<a href=3D"http://docs.yahoo.com/info/terms/">
Yahoo! Groups Terms of Use
</a> |
<a href=3D"mailto:[email protected]?subject=3DUns=
ubscribe">
Unsubscribe=20
</a>=20
<br>
</div>
<br>
<!-- |**|end egp html banner|**| -->
<div style=3D"color: white; clear: both;"/>__,_._,___</div>
</body></html>
--Apple-Mail=_CCCE357C-B909-415C-8F84-39549CDBFEE9--