You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Mark Volkmann <ma...@ociweb.com> on 2007/03/13 20:29:35 UTC
transactions with Spring DAOs
I seem to be getting automatic transactions when I use Spring DAOs
generated by Abator even though my SqlMapConfig.xml contains the
following.
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
...
Here's the simple bit of code I'm using to test this. The change to
addressId is persisted even though a RunTime exception is thrown
inside the try and commitTransaction is not called. Any idea what I
might be doing wrong?
boolean problem = true;
Person person = personDAO.selectByPrimaryKey(markId);
SqlMapClient smc = ((SqlMapClientDaoSupport)
personDAO).getSqlMapClient();
try {
smc.startTransaction();
person.setAddressId(addressId);
personDAO.updateByPrimaryKey(person);
System.out.println("updated address"); // This is output.
// What happens if an exception occurs here?
if (problem) throw new RuntimeException("something bad
happened");
person.setHomePhoneId(homePhoneId);
personDAO.updateByPrimaryKey(person);
System.out.println("updated home phone"); // This isn't output.
smc.commitTransaction();
} finally {
smc.endTransaction();
}
Re: transactions with Spring DAOs
Posted by Jonathan Slate <js...@mtl.mit.edu>.
I share your pain, I'm having the same problem using postgres, I've
verified that with straight JDBC, transactions work fine, but using
SqlMapClient startTransaction seems to be doing autocommit. Here's a
short example. You can see I've tried calling
client.getCurrentConnection().setAutoCommit(false); (which seems to be
on by default) but it does not help.
If I pass in a Hen with a list of Chicks where one chick's name is too
long I get an SQL exception, but the Hen is still updated (i.e. the
Hen's name is changed).
I'm going to keep playing with this... but any suggestions are welcome.
-Jonathan
public class HenDAOIbatis extends SqlMapClientDaoSupport{
public void save(Hen hen) throws SQLException {
SqlMapClientTemplate sqlTemplate = getSqlMapClientTemplate();
SqlMapClient client = sqlTemplate.getSqlMapClient();
try {
client.startTransaction();
client.getCurrentConnection().setAutoCommit(false);
sqlTemplate.update("updateHen", hen);
sqlTemplate.delete("deleteAllChicks", hen);
for (Chick chick : hen.getChicks()) {
chick.setHenID(hen.getId());
sqlTemplate.insert("addChick", chick);
}
client.commitTransaction();
}
finally {
client.endTransaction();
}
}
Mark Volkmann wrote:
> I wrote some JDBC code this morning to verify that the version of
> MySQL I'm using and the driver I'm using is handling transactions
> correctly and it is.
>
> That brings me back to Spring DAOs generated by Abator. I'm convinced
> that simply calling the SqlMapClient startTransaction method does not
> disable MySQL auto-commit. I must need to do something else to disable
> that, but I don't know what.
>
> I'll look into using Spring declarative transactions and see if that
> works, but I'd really like a solution using SqlMapClient
> startTransaction.
>
>
RE: transactions with Spring DAOs
Posted by Daniel Kalcevich <DK...@marketlinx.com>.
You might want to look at the Spring forum's for examples on how to setup Transactions with SQL Maps. Way back when that is what I did, and was able to get it working successfully. The following forum postings may help:
http://forum.springframework.org/showthread.php?t=19032
http://forum.springframework.org/showthread.php?t=13389
Daniel
________________________________
From: Chris Lamey [mailto:clamey@localmatters.com]
Sent: Wed 3/14/2007 7:50 AM
To: user-java@ibatis.apache.org
Subject: Re: transactions with Spring DAOs
I'd highly recommend using Spring's declarative transactions over
manually handling them in your code. The big benefit is that you can
string calls together in a single transaction without having to pass
transactions/connections/whatever around in your methods. Plus your
code is cleaner because there's no transaction code involved. It is a
little weird at first to "give up" control in the code, but it's very
cool when you see it working.
I wrote up a quick example in a thread on this list with a subject of
"Transaction issue with 2.2.0", if you want an example.
As for why the Spring SqlMapClient seems to be ignoring the SIMPLE/JDBC
iBATIS config, it might be that the SqlMapClient is using a DataSource
defined in Spring and not the iBATIS one? What does the bean element
that defines the SqlMapClient look like?
On Wed, 2007-03-14 at 09:01 -0500, Mark Volkmann wrote:
> I wrote some JDBC code this morning to verify that the version of
> MySQL I'm using and the driver I'm using is handling transactions
> correctly and it is.
>
> That brings me back to Spring DAOs generated by Abator. I'm convinced
> that simply calling the SqlMapClient startTransaction method does not
> disable MySQL auto-commit. I must need to do something else to
> disable that, but I don't know what.
>
> I'll look into using Spring declarative transactions and see if that
> works, but I'd really like a solution using SqlMapClient
> startTransaction.
>
Re: transactions with Spring DAOs
Posted by Chris Lamey <cl...@localmatters.com>.
I'd highly recommend using Spring's declarative transactions over
manually handling them in your code. The big benefit is that you can
string calls together in a single transaction without having to pass
transactions/connections/whatever around in your methods. Plus your
code is cleaner because there's no transaction code involved. It is a
little weird at first to "give up" control in the code, but it's very
cool when you see it working.
I wrote up a quick example in a thread on this list with a subject of
"Transaction issue with 2.2.0", if you want an example.
As for why the Spring SqlMapClient seems to be ignoring the SIMPLE/JDBC
iBATIS config, it might be that the SqlMapClient is using a DataSource
defined in Spring and not the iBATIS one? What does the bean element
that defines the SqlMapClient look like?
On Wed, 2007-03-14 at 09:01 -0500, Mark Volkmann wrote:
> I wrote some JDBC code this morning to verify that the version of
> MySQL I'm using and the driver I'm using is handling transactions
> correctly and it is.
>
> That brings me back to Spring DAOs generated by Abator. I'm convinced
> that simply calling the SqlMapClient startTransaction method does not
> disable MySQL auto-commit. I must need to do something else to
> disable that, but I don't know what.
>
> I'll look into using Spring declarative transactions and see if that
> works, but I'd really like a solution using SqlMapClient
> startTransaction.
>
Re: transactions with Spring DAOs
Posted by Mark Volkmann <ma...@ociweb.com>.
I wrote some JDBC code this morning to verify that the version of
MySQL I'm using and the driver I'm using is handling transactions
correctly and it is.
That brings me back to Spring DAOs generated by Abator. I'm convinced
that simply calling the SqlMapClient startTransaction method does not
disable MySQL auto-commit. I must need to do something else to
disable that, but I don't know what.
I'll look into using Spring declarative transactions and see if that
works, but I'd really like a solution using SqlMapClient
startTransaction.
Re: transactions with Spring DAOs
Posted by Mark Volkmann <ma...@ociweb.com>.
On Mar 13, 2007, at 8:37 PM, Daniel Kalcevich wrote:
> Is there a reason you were calling the start, end and commit
> transaction within Spring?
Do you mean why I'm I doing it in my Java code instead of doing it
declaratively?
The only reason is that I haven't learned how to do that yet.
If you know a good web page that shows how to do that, I'd love to
get the URL.
> We use DAO's with Spring as our TransactionManager. Not sure if
> you are using the SqlMapClientBean from Spring or not. But I
> noticed the transactions being declared, but also the fact we use
> the method getSqlMapClientTemplate(). Just some thoughts.
All things I don't know how to do yet, but I'm definitely open to
looking at alternatives, especially since what I'm doing isn't
working. Thanks for the tips! Any code examples would be greatly
appreciated!
>
> Daniel
>
> ________________________________
>
> From: Mark Volkmann [mailto:mark@ociweb.com]
> Sent: Tue 3/13/2007 6:34 PM
> To: user-java@ibatis.apache.org
> Subject: Re: transactions with Spring DAOs
>
>
>
> On Mar 13, 2007, at 5:30 PM, Larry Meadors wrote:
>
>> Version 5 will work for you, you just have to define the tables using
>> the innodb engine.
>>
>> http://dev.mysql.com/doc/refman/5.0/en/using-innodb-tables.html
>
> Thanks for the tips! I followed the directions, adding
> "ENGINE=InnoDB" to the end of all my table create commands, dropped
> the tables, and recreated them. My code still runs as if autocommit
> is enabled. I assumed that would be disabled just by explicitly
> demarcating the beginning of a transaction, but that alone isn't
> doing it.
>
> Is there a way I can verify that my tables are setup for InnoDB?
>
> Is there something else I need to do to disable autocommit for MySQL?
>
> Should I ditch MySQL and use Postgres? ;-)
>
>>
>> Larry
>>
>>
>> On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
>>> On Mar 13, 2007, at 3:04 PM, Larry Meadors wrote:
>>>
>>>> Are you using MySQL w/o innodb, or some other database that is not
>>>> transaction aware?
>>>
>>> Ahh ... that may be the issue. I'm using MySQL version 5.0.27-
>>> standard. I assumed that all newer versions of MySQL were
>>> transaction
>>> aware. Is that wrong? Perhaps I need to download a different
>>> version.
>>>
>>>> Larry
>>>>
>>>>
>>>> On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
>>>>> I seem to be getting automatic transactions when I use Spring DAOs
>>>>> generated by Abator even though my SqlMapConfig.xml contains the
>>>>> following.
>>>>>
>>>>> <transactionManager type="JDBC">
>>>>> <dataSource type="SIMPLE">
>>>>> ...
>>>>>
>>>>> Here's the simple bit of code I'm using to test this. The
>>> change to
>>>>> addressId is persisted even though a RunTime exception is thrown
>>>>> inside the try and commitTransaction is not called. Any idea
>>> what I
>>>>> might be doing wrong?
>>>>>
>>>>> boolean problem = true;
>>>>> Person person = personDAO.selectByPrimaryKey(markId);
>>>>> SqlMapClient smc = ((SqlMapClientDaoSupport)
>>>>> personDAO).getSqlMapClient();
>>>>> try {
>>>>> smc.startTransaction();
>>>>>
>>>>> person.setAddressId(addressId);
>>>>> personDAO.updateByPrimaryKey(person);
>>>>> System.out.println("updated address"); // This is output.
>>>>>
>>>>> // What happens if an exception occurs here?
>>>>> if (problem) throw new RuntimeException("something bad
>>>>> happened");
>>>>>
>>>>> person.setHomePhoneId(homePhoneId);
>>>>> personDAO.updateByPrimaryKey(person);
>>>>> System.out.println("updated home phone"); // This isn't
>>>>> output.
>>>>>
>>>>> smc.commitTransaction();
>>>>> } finally {
>>>>> smc.endTransaction();
>>>>> }
>>>>>
>>>
>>>
>
>
>
> <winmail.dat>
RE: transactions with Spring DAOs
Posted by Daniel Kalcevich <DK...@marketlinx.com>.
Is there a reason you were calling the start, end and commit transaction within Spring? We use DAO's with Spring as our TransactionManager. Not sure if you are using the SqlMapClientBean from Spring or not. But I noticed the transactions being declared, but also the fact we use the method getSqlMapClientTemplate(). Just some thoughts.
Daniel
________________________________
From: Mark Volkmann [mailto:mark@ociweb.com]
Sent: Tue 3/13/2007 6:34 PM
To: user-java@ibatis.apache.org
Subject: Re: transactions with Spring DAOs
On Mar 13, 2007, at 5:30 PM, Larry Meadors wrote:
> Version 5 will work for you, you just have to define the tables using
> the innodb engine.
>
> http://dev.mysql.com/doc/refman/5.0/en/using-innodb-tables.html
Thanks for the tips! I followed the directions, adding
"ENGINE=InnoDB" to the end of all my table create commands, dropped
the tables, and recreated them. My code still runs as if autocommit
is enabled. I assumed that would be disabled just by explicitly
demarcating the beginning of a transaction, but that alone isn't
doing it.
Is there a way I can verify that my tables are setup for InnoDB?
Is there something else I need to do to disable autocommit for MySQL?
Should I ditch MySQL and use Postgres? ;-)
>
> Larry
>
>
> On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
>> On Mar 13, 2007, at 3:04 PM, Larry Meadors wrote:
>>
>> > Are you using MySQL w/o innodb, or some other database that is not
>> > transaction aware?
>>
>> Ahh ... that may be the issue. I'm using MySQL version 5.0.27-
>> standard. I assumed that all newer versions of MySQL were transaction
>> aware. Is that wrong? Perhaps I need to download a different version.
>>
>> > Larry
>> >
>> >
>> > On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
>> >> I seem to be getting automatic transactions when I use Spring DAOs
>> >> generated by Abator even though my SqlMapConfig.xml contains the
>> >> following.
>> >>
>> >> <transactionManager type="JDBC">
>> >> <dataSource type="SIMPLE">
>> >> ...
>> >>
>> >> Here's the simple bit of code I'm using to test this. The
>> change to
>> >> addressId is persisted even though a RunTime exception is thrown
>> >> inside the try and commitTransaction is not called. Any idea
>> what I
>> >> might be doing wrong?
>> >>
>> >> boolean problem = true;
>> >> Person person = personDAO.selectByPrimaryKey(markId);
>> >> SqlMapClient smc = ((SqlMapClientDaoSupport)
>> >> personDAO).getSqlMapClient();
>> >> try {
>> >> smc.startTransaction();
>> >>
>> >> person.setAddressId(addressId);
>> >> personDAO.updateByPrimaryKey(person);
>> >> System.out.println("updated address"); // This is output.
>> >>
>> >> // What happens if an exception occurs here?
>> >> if (problem) throw new RuntimeException("something bad
>> >> happened");
>> >>
>> >> person.setHomePhoneId(homePhoneId);
>> >> personDAO.updateByPrimaryKey(person);
>> >> System.out.println("updated home phone"); // This isn't
>> >> output.
>> >>
>> >> smc.commitTransaction();
>> >> } finally {
>> >> smc.endTransaction();
>> >> }
>> >>
>>
>>
Re: transactions with Spring DAOs
Posted by Mark Volkmann <ma...@ociweb.com>.
On Mar 13, 2007, at 5:30 PM, Larry Meadors wrote:
> Version 5 will work for you, you just have to define the tables using
> the innodb engine.
>
> http://dev.mysql.com/doc/refman/5.0/en/using-innodb-tables.html
Thanks for the tips! I followed the directions, adding
"ENGINE=InnoDB" to the end of all my table create commands, dropped
the tables, and recreated them. My code still runs as if autocommit
is enabled. I assumed that would be disabled just by explicitly
demarcating the beginning of a transaction, but that alone isn't
doing it.
Is there a way I can verify that my tables are setup for InnoDB?
Is there something else I need to do to disable autocommit for MySQL?
Should I ditch MySQL and use Postgres? ;-)
>
> Larry
>
>
> On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
>> On Mar 13, 2007, at 3:04 PM, Larry Meadors wrote:
>>
>> > Are you using MySQL w/o innodb, or some other database that is not
>> > transaction aware?
>>
>> Ahh ... that may be the issue. I'm using MySQL version 5.0.27-
>> standard. I assumed that all newer versions of MySQL were transaction
>> aware. Is that wrong? Perhaps I need to download a different version.
>>
>> > Larry
>> >
>> >
>> > On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
>> >> I seem to be getting automatic transactions when I use Spring DAOs
>> >> generated by Abator even though my SqlMapConfig.xml contains the
>> >> following.
>> >>
>> >> <transactionManager type="JDBC">
>> >> <dataSource type="SIMPLE">
>> >> ...
>> >>
>> >> Here's the simple bit of code I'm using to test this. The
>> change to
>> >> addressId is persisted even though a RunTime exception is thrown
>> >> inside the try and commitTransaction is not called. Any idea
>> what I
>> >> might be doing wrong?
>> >>
>> >> boolean problem = true;
>> >> Person person = personDAO.selectByPrimaryKey(markId);
>> >> SqlMapClient smc = ((SqlMapClientDaoSupport)
>> >> personDAO).getSqlMapClient();
>> >> try {
>> >> smc.startTransaction();
>> >>
>> >> person.setAddressId(addressId);
>> >> personDAO.updateByPrimaryKey(person);
>> >> System.out.println("updated address"); // This is output.
>> >>
>> >> // What happens if an exception occurs here?
>> >> if (problem) throw new RuntimeException("something bad
>> >> happened");
>> >>
>> >> person.setHomePhoneId(homePhoneId);
>> >> personDAO.updateByPrimaryKey(person);
>> >> System.out.println("updated home phone"); // This isn't
>> >> output.
>> >>
>> >> smc.commitTransaction();
>> >> } finally {
>> >> smc.endTransaction();
>> >> }
>> >>
>>
>>
Re: transactions with Spring DAOs
Posted by Larry Meadors <lm...@apache.org>.
Version 5 will work for you, you just have to define the tables using
the innodb engine.
http://dev.mysql.com/doc/refman/5.0/en/using-innodb-tables.html
Larry
On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
> On Mar 13, 2007, at 3:04 PM, Larry Meadors wrote:
>
> > Are you using MySQL w/o innodb, or some other database that is not
> > transaction aware?
>
> Ahh ... that may be the issue. I'm using MySQL version 5.0.27-
> standard. I assumed that all newer versions of MySQL were transaction
> aware. Is that wrong? Perhaps I need to download a different version.
>
> > Larry
> >
> >
> > On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
> >> I seem to be getting automatic transactions when I use Spring DAOs
> >> generated by Abator even though my SqlMapConfig.xml contains the
> >> following.
> >>
> >> <transactionManager type="JDBC">
> >> <dataSource type="SIMPLE">
> >> ...
> >>
> >> Here's the simple bit of code I'm using to test this. The change to
> >> addressId is persisted even though a RunTime exception is thrown
> >> inside the try and commitTransaction is not called. Any idea what I
> >> might be doing wrong?
> >>
> >> boolean problem = true;
> >> Person person = personDAO.selectByPrimaryKey(markId);
> >> SqlMapClient smc = ((SqlMapClientDaoSupport)
> >> personDAO).getSqlMapClient();
> >> try {
> >> smc.startTransaction();
> >>
> >> person.setAddressId(addressId);
> >> personDAO.updateByPrimaryKey(person);
> >> System.out.println("updated address"); // This is output.
> >>
> >> // What happens if an exception occurs here?
> >> if (problem) throw new RuntimeException("something bad
> >> happened");
> >>
> >> person.setHomePhoneId(homePhoneId);
> >> personDAO.updateByPrimaryKey(person);
> >> System.out.println("updated home phone"); // This isn't
> >> output.
> >>
> >> smc.commitTransaction();
> >> } finally {
> >> smc.endTransaction();
> >> }
> >>
>
>
Re: transactions with Spring DAOs
Posted by Mark Volkmann <ma...@ociweb.com>.
On Mar 13, 2007, at 3:04 PM, Larry Meadors wrote:
> Are you using MySQL w/o innodb, or some other database that is not
> transaction aware?
Ahh ... that may be the issue. I'm using MySQL version 5.0.27-
standard. I assumed that all newer versions of MySQL were transaction
aware. Is that wrong? Perhaps I need to download a different version.
> Larry
>
>
> On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
>> I seem to be getting automatic transactions when I use Spring DAOs
>> generated by Abator even though my SqlMapConfig.xml contains the
>> following.
>>
>> <transactionManager type="JDBC">
>> <dataSource type="SIMPLE">
>> ...
>>
>> Here's the simple bit of code I'm using to test this. The change to
>> addressId is persisted even though a RunTime exception is thrown
>> inside the try and commitTransaction is not called. Any idea what I
>> might be doing wrong?
>>
>> boolean problem = true;
>> Person person = personDAO.selectByPrimaryKey(markId);
>> SqlMapClient smc = ((SqlMapClientDaoSupport)
>> personDAO).getSqlMapClient();
>> try {
>> smc.startTransaction();
>>
>> person.setAddressId(addressId);
>> personDAO.updateByPrimaryKey(person);
>> System.out.println("updated address"); // This is output.
>>
>> // What happens if an exception occurs here?
>> if (problem) throw new RuntimeException("something bad
>> happened");
>>
>> person.setHomePhoneId(homePhoneId);
>> personDAO.updateByPrimaryKey(person);
>> System.out.println("updated home phone"); // This isn't
>> output.
>>
>> smc.commitTransaction();
>> } finally {
>> smc.endTransaction();
>> }
>>
Re: transactions with Spring DAOs
Posted by Larry Meadors <lm...@apache.org>.
Are you using MySQL w/o innodb, or some other database that is not
transaction aware?
Larry
On 3/13/07, Mark Volkmann <ma...@ociweb.com> wrote:
> I seem to be getting automatic transactions when I use Spring DAOs
> generated by Abator even though my SqlMapConfig.xml contains the
> following.
>
> <transactionManager type="JDBC">
> <dataSource type="SIMPLE">
> ...
>
> Here's the simple bit of code I'm using to test this. The change to
> addressId is persisted even though a RunTime exception is thrown
> inside the try and commitTransaction is not called. Any idea what I
> might be doing wrong?
>
> boolean problem = true;
> Person person = personDAO.selectByPrimaryKey(markId);
> SqlMapClient smc = ((SqlMapClientDaoSupport)
> personDAO).getSqlMapClient();
> try {
> smc.startTransaction();
>
> person.setAddressId(addressId);
> personDAO.updateByPrimaryKey(person);
> System.out.println("updated address"); // This is output.
>
> // What happens if an exception occurs here?
> if (problem) throw new RuntimeException("something bad
> happened");
>
> person.setHomePhoneId(homePhoneId);
> personDAO.updateByPrimaryKey(person);
> System.out.println("updated home phone"); // This isn't output.
>
> smc.commitTransaction();
> } finally {
> smc.endTransaction();
> }
>