You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Guillaume Laforge <gl...@gmail.com> on 2005/08/05 16:12:13 UTC

PB and Oracle9i: transaction issue

Hi all,

I'm having some issues with the PersistenceBroker.

I've got a one-to-many relationship, say a sentence, which has got
localized labels (1 sentence <-> several multilingual labels).

I want to store a new sentence with a new set of labels.
But I want to do that in a single transaction, so that if something
fails, I can rollback everything.

So, somewhere in my code, I'm doing a broker.beginTransaction().
I store my sentence. I create some labels a bit further and store them.
And later, broker.commitTransaction().
And in case something fails, I'm rollback-ing the transaction in some
try catch finally surrounding my code.

If autocommit is true, then my sentences and labels are stored... but,
unfortunately, if something goes wrong, OJB won't rollback anything.
So autocommit to true is not a good option for me since I want a real
transaction (I also tried setting autocommit on the broker object
itself but that wasn't any better).

If I set useAutoCommit="2" in my jdbc-connection-descriptor, I store
my sentence, but just after I see a weird message in the logs:

accesslayer.ConnectionManagerImpl  - Release connection: connection is
in local transaction, missing 'localCommit' or 'localRollback' call -
try to rollback the connection

And then, when I try to store the first label, I get an exception:

org.apache.ojb.broker.KeyConstraintViolatedException: SQL failure
while insert object data for class com.foo.bar.Xxxx, PK of the given
object is [ id=201], object was xxxx, exception message is [ORA-02291:
integrity constraint  (AE6.SYS_C001821) violated - parent key not
found.

The message basically means that it can't find the sentence primary
key that the label is referencing (foreign key).

Background: I'm using OJB 1.0.3 and Oracle 9i with the thin driver.

So, I was wondering how I could store a new sentence and label in on
transaction and be able to rollback everything if I so desire?

-- 
Guillaume Laforge
http://glaforge.free.fr/blog/java

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Brian McCallister <br...@chariotsolutions.com>.
Mapping is biggest weakness in o/r mapping right now =(

Most issues come down to "not quite correct" mapping all too often =(

On Aug 10, 2005, at 11:22 AM, Guillaume Laforge wrote:

> Hi again,
>
> I hesitate to reveal the truth and expose how stupid I must be... but
> still... I think I've found the source of my problem. It seems as if
> it were a mere mapping issue!
>
> What a pity the messages are so misleading... I guess some sanity
> checker is in order so that we can compare a mapping and a database's
> schema (hint, hint Brian!).
>
> In my mapping, it seems like I had configured a many-to-many
> relationship, though what I really had was a simple one-to-many
> relationship: I had a foreignkey AND remote-foreignkey in my xdoclet
> tags pointing at the same column. Though obviously the
> remote-foreignkey wasn't needed at all.
>
> Then, I set back autocommit to 2 explicitely (to be sure I was in
> autocommit false), and tried again my little test... and... tada...
> all my objects were rollbacked as I expected!
>
> I'm sad I wasted that many hours on such a trivial problem :-(
> I apologize for having bothered you with somewhat of a false  
> problem :-(
>
> Anyway, thanks a lot for your great support and help.
>
> -- 
> Guillaume Laforge
> http://glaforge.free.fr/blog/groovy
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Brian McCallister <br...@apache.org>.
On Aug 10, 2005, at 11:22 AM, Guillaume Laforge wrote:

> What a pity the messages are so misleading... I guess some sanity
> checker is in order so that we can compare a mapping and a database's
> schema (hint, hint Brian!).

Okay, attached =)

Thank you to my previous employer (Fort Hill Company) for letting me  
distribute this!

-Brian




Re: PB and Oracle9i: transaction issue

Posted by Guillaume Laforge <gl...@gmail.com>.
> > WDYT, does it make sense to rework this to use the DdlUtils API and
> > make a Ant task from it ?
> >
> 
> We could, but I think it works better as a JUnit TestCase. It can be
> integrated right in with system tests, run from ant, etc, right now.
> 
> Understanding DdlUtils schemas would also be good, but I think that
> would be a separate test case =)

I'd tend to think an Ant task would be really great on the contrary.
Of course, having both is ideal, so we have the choice.
But adding one more little step in the build process makes more sense
and can break the build earlier if you notice something wrong in your
mapping.

-- 
Guillaume Laforge
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Brian McCallister <br...@chariotsolutions.com>.
On Aug 10, 2005, at 4:02 PM, Thomas Dudziak wrote:

>> Thank you to my previous employer (Fort Hill Company) for allowing me
>> to distribute this. I'll clean it up and put it in CVS for 1.0.4 when
>> It takes a PBKey and verifies that the metadata is sane for the
>> actual database schema (examined via JDBC metadata).
>>
>
> WDYT, does it make sense to rework this to use the DdlUtils API and
> make a Ant task from it ?
>

We could, but I think it works better as a JUnit TestCase. It can be  
integrated right in with system tests, run from ant, etc, right now.

Understanding DdlUtils schemas would also be good, but I think that  
would be a separate test case =)

-Brian

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Thomas Dudziak <to...@gmail.com>.
On 8/10/05, Brian McCallister <br...@apache.org> wrote:

> Okay =)
> 
> Thank you to my previous employer (Fort Hill Company) for allowing me
> to distribute this. I'll clean it up and put it in CVS for 1.0.4 when
> I get a chance, meanwhile (as the first email isn't making it through
> the list, probably the attachment)...
> 
> http://morphy.skife.org/TestOJBSchemaSanity.java
> 
> It takes a PBKey and verifies that the metadata is sane for the
> actual database schema (examined via JDBC metadata).

WDYT, does it make sense to rework this to use the DdlUtils API and
make a Ant task from it ?

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Brian McCallister <br...@apache.org>.
On Aug 10, 2005, at 11:22 AM, Guillaume Laforge wrote:

> What a pity the messages are so misleading... I guess some sanity
> checker is in order so that we can compare a mapping and a database's
> schema (hint, hint Brian!).


Okay =)

Thank you to my previous employer (Fort Hill Company) for allowing me  
to distribute this. I'll clean it up and put it in CVS for 1.0.4 when  
I get a chance, meanwhile (as the first email isn't making it through  
the list, probably the attachment)...

http://morphy.skife.org/TestOJBSchemaSanity.java

It takes a PBKey and verifies that the metadata is sane for the  
actual database schema (examined via JDBC metadata).

-Brian

Re: PB and Oracle9i: transaction issue

Posted by Guillaume Laforge <gl...@gmail.com>.
Tom,

On 10/08/05, Thomas Dudziak <to...@gmail.com> wrote:
> > But for my specific case, that may be handy to check that foreignkey
> > and remote-foreignkey don't point at the same column :-)
> >
> > In my xdoclet tag, I had both foreignkey="xxx" and
> > remote-foreignkey="xxx" (damn cut'n paste I guess).
> >
> > So that would be great if you could just add this little check that
> > verifies that you can't have those two attributes pointing at the same
> > column!
> 
> Right. Please add an issue for this and I'll add the check.

Done!
http://issues.apache.org/jira/browse/OJB-67

-- 
Guillaume Laforge
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Thomas Dudziak <to...@gmail.com>.
On 8/10/05, Guillaume Laforge <gl...@gmail.com> wrote:
> Hi Tom!
> 
> > If you add a JIRA feature request for the XDoclet module with some
> > sample code/mapping, I enhance the module so that it produces a
> > warning for such cases.
> 
> Hmmm, in fact, AFAIK, the XDoclet module won't connect to the database
> to check that the schema corresponds to the generated repository-user.
> So I'm not sure you can enhance the module in such a way that it
> notices a many-to-many relationship is a one-to-many or vice-versa.

while such a thing would be useful, I don't think that it is something
that the XDoclet module should do.
 
> But for my specific case, that may be handy to check that foreignkey
> and remote-foreignkey don't point at the same column :-)
> 
> In my xdoclet tag, I had both foreignkey="xxx" and
> remote-foreignkey="xxx" (damn cut'n paste I guess).
> 
> So that would be great if you could just add this little check that
> verifies that you can't have those two attributes pointing at the same
> column!

Right. Please add an issue for this and I'll add the check.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Guillaume Laforge <gl...@gmail.com>.
Hi Tom!

> If you add a JIRA feature request for the XDoclet module with some
> sample code/mapping, I enhance the module so that it produces a
> warning for such cases.

Hmmm, in fact, AFAIK, the XDoclet module won't connect to the database
to check that the schema corresponds to the generated repository-user.
So I'm not sure you can enhance the module in such a way that it
notices a many-to-many relationship is a one-to-many or vice-versa.

But for my specific case, that may be handy to check that foreignkey
and remote-foreignkey don't point at the same column :-)

In my xdoclet tag, I had both foreignkey="xxx" and
remote-foreignkey="xxx" (damn cut'n paste I guess).

So that would be great if you could just add this little check that
verifies that you can't have those two attributes pointing at the same
column!

-- 
Guillaume Laforge
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Thomas Dudziak <to...@gmail.com>.
On 8/10/05, Guillaume Laforge <gl...@gmail.com> wrote:

> I hesitate to reveal the truth and expose how stupid I must be... but
> still... I think I've found the source of my problem. It seems as if
> it were a mere mapping issue!
> 
> What a pity the messages are so misleading... I guess some sanity
> checker is in order so that we can compare a mapping and a database's
> schema (hint, hint Brian!).
> 
> In my mapping, it seems like I had configured a many-to-many
> relationship, though what I really had was a simple one-to-many
> relationship: I had a foreignkey AND remote-foreignkey in my xdoclet
> tags pointing at the same column. Though obviously the
> remote-foreignkey wasn't needed at all.

If you add a JIRA feature request for the XDoclet module with some
sample code/mapping, I enhance the module so that it produces a
warning for such cases.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Guillaume Laforge <gl...@gmail.com>.
Hi again,

I hesitate to reveal the truth and expose how stupid I must be... but
still... I think I've found the source of my problem. It seems as if
it were a mere mapping issue!

What a pity the messages are so misleading... I guess some sanity
checker is in order so that we can compare a mapping and a database's
schema (hint, hint Brian!).

In my mapping, it seems like I had configured a many-to-many
relationship, though what I really had was a simple one-to-many
relationship: I had a foreignkey AND remote-foreignkey in my xdoclet
tags pointing at the same column. Though obviously the
remote-foreignkey wasn't needed at all.

Then, I set back autocommit to 2 explicitely (to be sure I was in
autocommit false), and tried again my little test... and... tada...
all my objects were rollbacked as I expected!

I'm sad I wasted that many hours on such a trivial problem :-(
I apologize for having bothered you with somewhat of a false problem :-(

Anyway, thanks a lot for your great support and help.

-- 
Guillaume Laforge
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Guillaume Laforge <gl...@gmail.com>.
On 10/08/05, Armin Waibel <ar...@apache.org> wrote:
> I'm clueless too.
> ConnectionFactoryManagedImpl is only needed when delegate the
> tx-demarcation to JTA-api. I assume you have checked the datasource
> settings in tomcat and the tx-settings in your DB.

Okay, I see.

> This is really interesting, because OJB seems to release a connection
> while a running PB-tx. ConnectionManager#releaseConnection was called by
> PersistenceBroker#close() method.
> Do you close the used PB instance before ending of PB-tx demarcation end?

Nope.

> Do you use PersistenceBrokerFactoryClass=org.apache.ojb.broker.core.PersistenceBrokerFactorySyncImpl
> in OJB.properties file?

Nope.

Damn! :-(
I hope I'll find some solution...

-- 
Guillaume Laforge
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Armin Waibel <ar...@apache.org>.
Hi Guillaume,

 > I also tried a different connection factory:
 > 
ConnectionFactoryClass=org.apache.ojb.broker.accesslayer.ConnectionFactoryManagedImpl
 > instead of the one I was using:
 > 
ConnectionFactoryClass=org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl
 > (in case my Tomcat 5.0 is in cause somehow with my datasource
 > definition or what not).
 >
 > But I'm really clueless to what's happening there :-(

I'm clueless too.
ConnectionFactoryManagedImpl is only needed when delegate the 
tx-demarcation to JTA-api. I assume you have checked the datasource 
settings in tomcat and the tx-settings in your DB.

 > What's odd also is this line in my debug logs:
 > ConnectionManagerImpl  - Release connection: connection is in local
 > transaction, missing 'localCommit' or 'localRollback' call - try to
 > rollback the connection
 >

This is really interesting, because OJB seems to release a connection 
while a running PB-tx. ConnectionManager#releaseConnection was called by 
PersistenceBroker#close() method.
Do you close the used PB instance before ending of PB-tx demarcation end?
Do you use
PersistenceBrokerFactoryClass=org.apache.ojb.broker.core.PersistenceBrokerFactorySyncImpl
in OJB.properties file?

regards,
Armin


Guillaume Laforge wrote:
> Hi Armin,
> 
> On 05/08/05, Armin Waibel <ar...@apache.org> wrote:
> 
>>if you set "useAutoCommit=1", OJB set the connection autoCommit
>>attribute to 'false' when PB-tx demarcation is used. After the PB-tx the
>>autoCommit attribute is reset to 'true'.
>>http://db.apache.org/ojb/docu/guides/repository.html#useAutoCommit
>>
>>Everything done between PB.begin... and PB.commit... will use the same
>>connection with autoCommit 'false', thus on rollback nothing will be
>>written to DB.
> 
> 
> Thanks for your explanations.
> 
> That's pretty odd, but I still can't manage to make transactions work properly.
> 
> I tried the various autocommit options (0, 1, 2).
> 
> I also tried specifying explicitely the autocommit with:
> broker.serviceConnectionManager().getConnection().setAutoCommit(false);
> 
> I tried changing my mapping (auto-insert/update/etc).
> 
> I also tried a different connection factory:
> ConnectionFactoryClass=org.apache.ojb.broker.accesslayer.ConnectionFactoryManagedImpl
> instead of the one I was using:
> ConnectionFactoryClass=org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl
> (in case my Tomcat 5.0 is in cause somehow with my datasource
> definition or what not).
> 
> But I'm really clueless to what's happening there :-(
> 
> Even though I'm in between a beginTransaction and commitTransaction,
> though there's just a few select queries, plus the broker.store()
> operations, if there's some exception being thrown and I call
> abortTransaction() in my catch block, it won't rollback all the stored
> objects in my transaction -- if I can still call it a transaction
> since it's not one anymore :-(
> 
> I wanted to make a smaller sample outside of a container, with an
> ojb-blank app, but couldn't make it work on the command-line, so I
> didn't pursue further.
> 
> What's odd also is this line in my debug logs:
> ConnectionManagerImpl  - Release connection: connection is in local
> transaction, missing 'localCommit' or 'localRollback' call - try to
> rollback the connection
> 
> I see it just after I store my first object in my transaction. I'm
> sure there's something about that, and I guess it's not normal.
> 
> If I set autocommit to 2 explicitely, I don't see it, but as soon as I
> store my second object (depending on the first one with a FK), I get a
> stacktrace saying it cannot insert my second object because the parent
> one doesn't exist :-(
> 
> Any idea what could be wrong?
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Guillaume Laforge <gl...@gmail.com>.
Hi Armin,

On 05/08/05, Armin Waibel <ar...@apache.org> wrote:
> if you set "useAutoCommit=1", OJB set the connection autoCommit
> attribute to 'false' when PB-tx demarcation is used. After the PB-tx the
> autoCommit attribute is reset to 'true'.
> http://db.apache.org/ojb/docu/guides/repository.html#useAutoCommit
> 
> Everything done between PB.begin... and PB.commit... will use the same
> connection with autoCommit 'false', thus on rollback nothing will be
> written to DB.

Thanks for your explanations.

That's pretty odd, but I still can't manage to make transactions work properly.

I tried the various autocommit options (0, 1, 2).

I also tried specifying explicitely the autocommit with:
broker.serviceConnectionManager().getConnection().setAutoCommit(false);

I tried changing my mapping (auto-insert/update/etc).

I also tried a different connection factory:
ConnectionFactoryClass=org.apache.ojb.broker.accesslayer.ConnectionFactoryManagedImpl
instead of the one I was using:
ConnectionFactoryClass=org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl
(in case my Tomcat 5.0 is in cause somehow with my datasource
definition or what not).

But I'm really clueless to what's happening there :-(

Even though I'm in between a beginTransaction and commitTransaction,
though there's just a few select queries, plus the broker.store()
operations, if there's some exception being thrown and I call
abortTransaction() in my catch block, it won't rollback all the stored
objects in my transaction -- if I can still call it a transaction
since it's not one anymore :-(

I wanted to make a smaller sample outside of a container, with an
ojb-blank app, but couldn't make it work on the command-line, so I
didn't pursue further.

What's odd also is this line in my debug logs:
ConnectionManagerImpl  - Release connection: connection is in local
transaction, missing 'localCommit' or 'localRollback' call - try to
rollback the connection

I see it just after I store my first object in my transaction. I'm
sure there's something about that, and I guess it's not normal.

If I set autocommit to 2 explicitely, I don't see it, but as soon as I
store my second object (depending on the first one with a FK), I get a
stacktrace saying it cannot insert my second object because the parent
one doesn't exist :-(

Any idea what could be wrong?

-- 
Guillaume Laforge
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: PB and Oracle9i: transaction issue

Posted by Armin Waibel <ar...@apache.org>.
Hi,

if you set "useAutoCommit=1", OJB set the connection autoCommit 
attribute to 'false' when PB-tx demarcation is used. After the PB-tx the 
autoCommit attribute is reset to 'true'.
http://db.apache.org/ojb/docu/guides/repository.html#useAutoCommit

Everything done between PB.begin... and PB.commit... will use the same 
connection with autoCommit 'false', thus on rollback nothing will be 
written to DB.

regards,
Armin



Guillaume Laforge wrote:
> Hi all,
> 
> I'm having some issues with the PersistenceBroker.
> 
> I've got a one-to-many relationship, say a sentence, which has got
> localized labels (1 sentence <-> several multilingual labels).
> 
> I want to store a new sentence with a new set of labels.
> But I want to do that in a single transaction, so that if something
> fails, I can rollback everything.
> 
> So, somewhere in my code, I'm doing a broker.beginTransaction().
> I store my sentence. I create some labels a bit further and store them.
> And later, broker.commitTransaction().
> And in case something fails, I'm rollback-ing the transaction in some
> try catch finally surrounding my code.
> 
> If autocommit is true, then my sentences and labels are stored... but,
> unfortunately, if something goes wrong, OJB won't rollback anything.
> So autocommit to true is not a good option for me since I want a real
> transaction (I also tried setting autocommit on the broker object
> itself but that wasn't any better).
> 
> If I set useAutoCommit="2" in my jdbc-connection-descriptor, I store
> my sentence, but just after I see a weird message in the logs:
> 
> accesslayer.ConnectionManagerImpl  - Release connection: connection is
> in local transaction, missing 'localCommit' or 'localRollback' call -
> try to rollback the connection
> 
> And then, when I try to store the first label, I get an exception:
> 
> org.apache.ojb.broker.KeyConstraintViolatedException: SQL failure
> while insert object data for class com.foo.bar.Xxxx, PK of the given
> object is [ id=201], object was xxxx, exception message is [ORA-02291:
> integrity constraint  (AE6.SYS_C001821) violated - parent key not
> found.
> 
> The message basically means that it can't find the sentence primary
> key that the label is referencing (foreign key).
> 
> Background: I'm using OJB 1.0.3 and Oracle 9i with the thin driver.
> 
> So, I was wondering how I could store a new sentence and label in on
> transaction and be able to rollback everything if I so desire?
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org