-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: I confused about autocommit set in hibernate source code
PostPosted: Sat Mar 27, 2004 8:47 am 
Newbie

Joined: Thu Mar 18, 2004 5:06 am
Posts: 5
Location: Tian Jin University of China
I confused about autocommit set in hibernate source code:

in DatasourceConnectionProvider.java
Quote:
public Connection getConnection() {
...
return ds.getConnection();
}

but in DriverManagerConnectionProvider.java
Quote:
public Connection getConnection(){

...
if ( pooled.getAutoCommit() ) pooled.setAutoCommit(false);
return pooled;
}


So there are errors in a transaction when using app server datasource in hibernate.cfg.xml and it is ok when using connections properties.The file I used was like:
Quote:

<hibernate-configuration>
<session-factory name="java:/hibernate/SessionFactory">
<!--ERROR-->
<property name="connection.datasource">java:/YWAFDS
</property>
<property name="connection.provider_class">net.sf.hibernate.connection.DatasourceConnectionProvider
</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect
</property>
<!--
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.8.1:3306/ywaf?useUnicode=true&amp;characterEncoding=GB2312
</property>
<property name="hibernate.connection.username">ywaf</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">100</property>
-->
<!-- Mapping files -->
<mapping resource="User.hbm"/>
</session-factory>
</hibernate-configuration>

why?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 27, 2004 11:26 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
Perhaps the hibernate team assumed that anyone advanced enough to use a datasource would also be advanced enough to turn autocommit off (thus making it unnecessary to explicitly test for it in the hibernate code)?

Autocommit is useful when working with the command-line SQL tool that comes with your database, but for building actual applications it's a fundamentally broken approach, IMHO.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 27, 2004 3:10 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
An application server should not give you a JDBC connection with autocommit set to on, if you think about it.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 28, 2004 6:03 am 
Newbie

Joined: Thu Mar 18, 2004 5:06 am
Posts: 5
Location: Tian Jin University of China
Hibernate can configured to use JDBC transaction or JTA transaction,but
the two way are inconsisent. Consider the following codes:
in a JDBC transaction:
Code:
Session session = sf.openSession();
Transaction tx = session.beginTransactioin();
...
session.flush();
tx.commit();
session.close();

but in a JTA transaction:
Code:
javax.transaction.UserTransaction tx = new InitialContext().lookup("javax.transaction.UserTransaction");
Session s1 = sf.openSession();
...
s1.flush();
s1.close();
...
Session s2 = sf.openSession();
...
s2.flush();
s2.close();

tx.commit();

So I must choose a type of transaction hardcoded in my program and change between them in the future are become impossible,is it?

_________________
Nothing is impossible to a willing heart


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 6:22 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
christian wrote:
An application server should not give you a JDBC connection with autocommit set to on, if you think about it.


I don't know if you're answering to me or to jian_xiao, but that was exactly the point I was trying to make. ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 6:43 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
jian_xiao wrote:
Hibernate can configured to use JDBC transaction or JTA transaction,but
the two way are inconsisent. Consider the following codes:
in a JDBC transaction:


Sorry, I don't understand your point. AFAIK, the hibernate transaction API is a wrapper around either the JDBC transaction API or JTA, depending on how you configure it. So if you configure hibernate to use JTA transactions, you can use the Hibernate transaction API and behind the scene JTA is used.

A few other points:

1. When using the Hibernate API, committing a transaction automatically flushes the session, so there is no need to explicitly call flush().

2. You second example seems to use the session-per-operation antipattern. Unless you have _very_ good reasons to do otherwise, I think it would be a good idea to follow the guidelines at
http://www.hibernate.org/168.html

3. Using declarative transactions, supported e.g. by the spring framework, you would avoid a lot of boring and repetetive code.

Quote:
So I must choose a type of transaction hardcoded in my program and change between them in the future are become impossible,is it?


Hey, as your very own signature says:

Quote:
Nothing is impossible to a willing heart


;-)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 9:31 pm 
Newbie

Joined: Thu Mar 18, 2004 5:06 am
Posts: 5
Location: Tian Jin University of China
Thank you,joib! I have read the articles you recommended,and these ideas really benefit me greatly!

_________________
Nothing is impossible to a willing heart


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 12:12 am 
Newbie

Joined: Wed Feb 25, 2004 11:41 pm
Posts: 19
Location: China
jian_xiao wrote:
Thank you,joib! I have read the articles you recommended,and these ideas really benefit me greatly!


hi Xiao Jian

come here
http://forum.javaeye.com

some great guys talking about hibernate in chinese^_^

btw:I dont think JTA surport in hibernate is perfect for your rigorous APP

_________________
who can tell me why?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.