File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate and web services Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate and web services" Watch "Hibernate and web services" New topic
Author

Hibernate and web services

Robin Stone
Greenhorn

Joined: Sep 13, 2013
Posts: 7
Hello, all,

I've been using these forums for a while now, and they always prove very useful - thank you!

However, I now have an issue I can't find the answer for. Perhaps it's because I don't know where to look.

I'm working with Hibernate. I have my methods working, and all is good.

I want to now create a web service based on these methods - I have not done this before, so am just practicing. I am using JBoss 5 and Axis in order to fit in with a framework.

When I deploy the web service, with a method that simply returns a String, it works fine every time.

However, as soon as I make a connection to a database in the same method, I get the following error:



The web method I'm calling is this:



If I remove the four session lines, it works fine.

The hibernate cfg that I'm using is:



If anyone could help with this error, it would be hugely appreciated. I just don't understand why the database connections are causing an issue...I guess it is something to do with the cfg file, but I do not know what.

Thank you so much in advance for any help.

Robin
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41477
    
  51
Which object is null?


Ping & DNS - my free Android networking tools app
Robin Stone
Greenhorn

Joined: Sep 13, 2013
Posts: 7
Ulf Dittmer wrote:Which object is null?


I had a look into this, and added some outputs after each line of code. The odd thing is that the programme ran the first time, returning the expected response.

But every other time, the outputs in the console window didn't refresh, and that's when I got the response null pointer. It was as if the web service had just stopped working.

I'm using the Web service explorer in Eclipse, but had previously tried with SoapUI too, which did give me the same error.

Is there any reason why after an initial call, it would no longer work?
Robin Stone
Greenhorn

Joined: Sep 13, 2013
Posts: 7
Hello again,

I've isolated the issue to this line (which I never thought would be a problem):



If I don't include this line, I have no issues...but surely this line is necessary!

Is there something I'm missing? Surely I need this line of code...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41477
    
  51
Why do you think it's necessary to close the session factory?

But I don't understand those lines of code at all - it seems you're starting a transaction, and then immediately commit it, before anything could have happened in it. What is that supposed to accomplish?
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5


On line 6, you are closing the SessionFactory when I think you should be closing the Session instead.

Ulf makes a very good point about doing nothing within your transaction.
Robin Stone
Greenhorn

Joined: Sep 13, 2013
Posts: 7
Ulf Dittmer wrote:Why do you think it's necessary to close the session factory?

But I don't understand those lines of code at all - it seems you're starting a transaction, and then immediately commit it, before anything could have happened in it. What is that supposed to accomplish?


I thought/think it is necessary because when I did a tutorial, that is how they finished the hibernate interaction.

Is it not necessary?

The code doesn't do anything because I'm trying to isolate the issue I'm having. So I stripped out all the code that actually does something, and found that the error was still happening with just those lines of code. As you now saw, I realised it was that one specific line that was causing the problem.
Robin Stone
Greenhorn

Joined: Sep 13, 2013
Posts: 7
James Boswell wrote:
On line 6, you are closing the SessionFactory when I think you should be closing the Session instead.

Ulf makes a very good point about doing nothing within your transaction.


Thank you for your suggestion. I will try this and get back - I am having issues with my server at the moment, so can not make any requests. One thing after another!

So what is the difference then? Why is closing the factory causing issues like this? Do you never need to close the factory like I'm doing, and just close the sessions?

As for my code, I had stripped out the meat of the code to isolate the error.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5


that is how they finished the hibernate interaction

Well I cannot think of a reason to close the SessionFactory while your application is running. SessionFactory is quite heavyweight so I would have thought closing and re-opening one would be expensive.

The Javadocs for the class also imply that an application will only have a single instance:
SessionFactory Javadoc
Robin Stone
Greenhorn

Joined: Sep 13, 2013
Posts: 7
James Boswell wrote:

that is how they finished the hibernate interaction

Well I cannot think of a reason to close the SessionFactory while your application is running. SessionFactory is quite heavyweight so I would have thought closing and re-opening one would be expensive.

The Javadocs for the class also imply that an application will only have a single instance:
SessionFactory Javadoc


Thank you for that. I realise that closing the factory may have been a bit much, but it looks like the code should still work from what I can tell...

I have tried your suggestion regarding closing the session, and still not joy...
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

When using Session, what exception do you get?
Robin Stone
Greenhorn

Joined: Sep 13, 2013
Posts: 7
Hello, everyone,

I just thought I'd let you know that I sorted my issue.

In the end, I moved from JBoss to Tomcat, and got far better errors.

Before this, I discovered that commiting the session also closed it, so when I was closing it afterwards, it raised an error when published as a web service; it didn't error at this when I was running it as a regular java application.

This was the major issue I was having. I also found that using a properties file for values wasn't liked by the program being a web service either, but this didn't take long to discover.

So all is good now; thank you for your help, everyone.

Robin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate and web services