-->
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.  [ 36 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Really disappointing! using JNDI Datasource within Jboss :(
PostPosted: Thu Nov 13, 2003 7:37 am 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
Hi,

I'm bloody new to Hibernate but I chose to use Hibernate over ORB...
well even before persisting my first Object I ran into a problem where I don't know how to solve it:

I defined a MySQL datasource in JBoss which I pass to Hibernate via JNDI.
The connection works fine when doing manual SQL statements!
When using Hibernate I run into the following error:
Code:
java.sql.SQLException: Can't call commit when autocommit=true


Fine, there is no possibility in Jboss or in Hibernates config to explicitely make the connection not auto committing


Seqentially I do the following:

Code:
Session s = applicationGlobalData.sessionFactory.openSession();
Transaction t = s.beginTransaction();
         
EntityAddress entityAddress = new EntityAddress();

[...]

s.save(entityAddress)
t.commit(); // flush the Session and commit the transaction
s.close();   



I even ask myself how Hibernate can offer a rollback functionality if it
doesn't care about autocommit?!

What can I do?

T.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 8:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I'm sorry, I can't quite determine the reason for your post. Were you insulting us, or looking for help? It surely must be one or the other. You couldn't possibly be trying to do both together.....


Top
 Profile  
 
 Post subject: Help is needed
PostPosted: Thu Nov 13, 2003 9:16 am 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
Hi,

Well I'm looking essentially for help because I really don't know what to do now. Ok I could retrieve the con from JNDI by myself, setting it to autocommit=off and passing it to Hibernate via a user defined connection - but that's not really elegant I think.

But what makes me even more afraid is that nobody in this forum seemed to have my problem before which means that the way I use Hibernate - in JSPs - is not really ubiquitous?!

T.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 10:06 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Just an architectural viewpoint on Hibernate, or any other DB connectivity, in JSPs...

I believe it wrong to have any form of database connectivity from your front end. The reasons are varied, but one would be, that if the data on the page is generated from a query in the JSP, then everytime the page is refreshed, the query runs and the database takes a hit. Enough refreshes, and you're going to have some issues.

I would rather recommend, that you introduce a good multi-tiered design, and have your Hibernate saving/loading, etc. in a datalayer, while having a nice, simple servlet interface between your JSPs and the datalayer. This would mean, that your datalayer is totally seperate, and can be reused by whichever front-end you want to use.

You datalayer can be an stateless session bean, or whatever you wish it to be, just I think it is best to keep database calls far away from the front end. :)

-G


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 10:32 am 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
Hi,

Well in the first step I want to keep things simple and for the few Forms I have: each hit of the submit button will trigger the creation of a new object and the persistence of it (well which doensn't work)
So I don't think there must be another layer inbetween.

Regarding your other thougths:
Does it really make sense to use Hibernate in Conjunction with EJBs?!?
Ok, I did never something with it but I think of it as something like a network transparent Hibernate and when using CMP why would you need Hibernate?!

STILL MY PROBLEM: How do I make my con auto committ = false ?

+

HOW can Hibernate offer roll back if it uses a autocommitted connection - that look braindead to me!!!

T.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 10:46 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
RAPHEAD wrote:
Hi,
Does it really make sense to use Hibernate in Conjunction with EJBs?!?
Ok, I did never something with it but I think of it as something like a network transparent Hibernate and when using CMP why would you need Hibernate?!

I didn't say EJBs. I said a Stateless Session bean, that simply performs work, not an EJB that requires CMP or BMP. Besides, I only mentioned that as a possibility.

And for auto commit off...well, do this:

Code:
Session session = sessionFactory.openSession();
session.connection().setAutoCommit(false);
Transasction tx = session.beginTransaction();


Or you can go read section 7.9.4 Exception handling in the Hibernate reference document (Ver 2.0.2)

-G


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 10:59 am 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
Ok, regarding the xJB stuff

But sorry, I'm really stuck!

HOW can I turn auto committ to off?! HOW?

Hibernate gets my connection like this:

Code:
<hibernate-configuration>
  <session-factory>
    <property name="connection.datasource">java:/mysqlds</property>
    <property name="show_sql">true</property>
    <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
    <property name="use_outer_join">false</property>
   
    <!-- Mapping files -->
    <mapping resource="com/dcx/Customer.hbm.xml"/>
    <mapping resource="com/dcx/EntityAddress.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


And as mentioned: ORB hase a similar mapping/configuration where they allow you to explicitely turn ac=off

Or is my code wrong?!

T.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 11:16 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Add "relaxAutocommit=true" to your JDBC URL. Read the documentation for the MySQL JDBC driver or try Google. This is not a Hibernate issue.

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 11:43 am 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
Hi Christian, Thanks yor your reply!

I read through the MySQL Connector j manuals and I thought about AND tried it - no success.

And this maked sense as this setting is only usefule if the driver does not support transactions (mine does)
Quote:
If the version of MySQL the driver connects to does not support transactions, allow calls to commit, rollback and setAutoCommit? (true/false)



And my second question is still not answered:
Suppose Hibernate lives with a autocommitted con and everything works fine.
One day there is an exception and my app issues an tx.rollback().
HOW can Hibernate possibly maintain the transaction when there is no underlying JDBC transaction?
It would have to undo all autocommitted changes which it is not likely to be able of!
Thus it is the job of Hibernate to make sure that the con is not auto committing - or is such a scenario really properly handled by Hibernate?


T.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 11:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Clearly it is your job to make sure that the appserver datasource that Hibernate uses has autocommit=false. And then rollbacks work correctly.

I am really mystified as to how could be problematic.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 11:54 am 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
Hi gavin,

First of all I'm happy, that my thoughts are right!

So the next step is to get this connection not autocommitting
My hope was that in this forum I'll find people who have similar problems but all my searches failed. Then I checked the settings for the Jboss datasources and the Hibernate config - none of them offers the facility to make this setting.
So what can I do?

Why isn't this problem ubiquitous? Is it the case that other JDBC drivers deliver none auto committ conns by default?

T.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 12:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
So the next step is to get this connection not autocommitting



And Christian just told you how to do this, did he not??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 12:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
P.S. Bill Burke tells me that JBoss disables autocommit by default when a connection is used in the context of a JTA transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 12:56 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Actually, the "relaxAutocommit" thing only allows you to commit()/whatever even if autocommit is on.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 4:58 am 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
Sorry Gavin,

As I said: Christians approach did NOT work I've tried it even before.

I'm not Using JTA!

Now I tried to include

Quote:
<property name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory
</property>


In my Hibernate config but now it become even worse:



Quote:
ERROR [SessionFactoryImpl] TransactionFactory class not found
java.lang.ClassNotFoundException: net/sf/hibernate/transaction/JDBCTransactionFactory

at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at net.sf.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:268)
at net.sf.hibernate.impl.SessionFactoryImpl.buildTransactionFactory(SessionFactoryImpl.java:463)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:191)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:627)
at com.dcx.ApplicationGlobalData.init(ApplicationGlobalData.java:55)
...


Please check:

http://db.apache.org/ojb/deployment.html

under '7.'

You'll see how that they have the facility to nail it to autocommit=false


Seems like a blind alley here...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 36 posts ]  Go to page 1, 2, 3  Next

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.