0

I have a Spring/Roo application which uses PostgreSQL and Hibernate.

As is appropriate, the connection information is located in the database properties file

src/main/resources/META-INF/spring/database.properties

Unfortunately, I have a situation where querying the database through Hibernate is draining the resources too much. I am sure that I can extract the database information (url/username/password) from the file listed above, but I am not sure where to begin my search.

Is there a manual or otherwise where I can find this information?

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
chora
  • 25
  • 8

1 Answers1

0

If you wish to bypass Hibernate to write more efficient queries by hand, you don't have to make separate connections to do it, and should not do so.

Get a Hibernate session and unwrap it to get the underlying java.sql.Connection. Or use native SQL via Hibernate's own interfaces.

That way you still get to use the useful bits of Hibernate, like the connection pooling integration. Sharing the same connection pool as Hibernate will improve efficiency, and you'll have a lot less extra code if you do it this way.

I haven't used Spring Roo, so I can't speak specifically for it. Here's info for Hibernate used via JPA or here. For direct Hibernate usage where you have a Session object, use Session.connection() on old versions of Hibernate, or the Work interface on newer versions:

If you insist on doing this by hand anyway, start with ClassLoader.getResource(...).

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • Thank you for your assistance. I did try the links you provided, but you are correct, that the efficiency was not worth the extra headache. – chora Feb 07 '14 at 18:49
  • @chora Sometimes it's *incredibly* worth the headache, particularly where using native features of the underlying database engine make it possible to write queries in much smarter and more efficient ways. If you're just trying to reduce per-query overhead then no, it's generally not worth it, and you'll get better results from making your queries smarter - set operation queries instead of looping over a query, etc. – Craig Ringer Feb 08 '14 at 08:25
  • Considering that I am working with a Spring/Roo/Hibernate application .. You are talking about putting "@Transactional(readOnly = true)" at the beginning of the function ... yes? – chora Feb 10 '14 at 16:44
  • @chora. No, I mean doing fewer smarter queries. – Craig Ringer Feb 10 '14 at 23:07
  • Gotcha. I am already doing that. I have gone so far as to creating several materialized views to make the queries run faster. Thank you very much for your views and helpful information. – chora Feb 10 '14 at 23:30
  • @chora If you've got particular problem queries you can always post on dba.stackexchange.com too - a good question with proper sample data, query text, `explain (buffers, analyze)` output etc is considered light entertainment over there ;-) – Craig Ringer Feb 10 '14 at 23:31