Results 1 to 6 of 6

Thread: HibernateTemplate + JdbcTemplate + HibernateTransactionMgr

  1. #1

    Default HibernateTemplate + JdbcTemplate + HibernateTransactionMgr

    Hi,
    I have a DAO based on HibernateDaoSupport and configured to use
    a HibernateTransactionMgr. The DAO does not have direct access to the datasource.
    Now, I would like to perform direct SQL commands in one of my HibernateCallbacks, and using the convenience provided by JdbcTemplate at the same time.
    Since I don't have a data source, I'm thinking of using the SingleConnectionDataSource to create the JdbcTemplate, like this:
    new JdbcTemplate(new SingleConnectionDataSource(session.connection()));

    Should be working, but I'm wondering, is there a better way? I see from the documentation that SingleConnectionDataSource is meant primarily for testing...

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Why not use a simple DataSource like Commons DBCP BasicDataSource? Then you get pooling, in any environment. You don't need JTA or an application server to have a pooling datasource...
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3

    Default

    Maybe I wans't clear... what I want to do is something like this:

    Code:
    void myHibernateDaoMethod(...) {
      getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session s) throws HibernateException {
            // do something with the session
            JDBCTemplate template = new JDBCTemplate(???);
            template.xxx
            // do something with the session
        }
      });
    }
    That is, I want to mix in the same method call both Hibernate and JDBC (I'm aware it may be dangerous, but I won't change data in the database
    that has already been loaded by the hibernate sesssion).

    The problem is just, JDBCTemplate wants a data source and doesn't seem happy to get just a connection, so I wanted to wrap the session's connection into a SingleConnectionDataSource.

    I was just wondering if there is a cleaner way, it seems kind of an unforeseen use of SingleConnectionDataSource.

  4. #4

    Default

    Ah, now I see the source of the misanderstanding. When I say I don't have a data source, I mean I don't have a direct reference to a DataSource object in my DAO since the DAOs are created by a HibernateTransactionProxy and receive just a connection factory (that won't let me get his data source).

  5. #5
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Why not simply declaring JdbcTemplate as an instance in your DAO class and let Spring wire it up?

  6. #6

    Default

    You mean, I create the JDBCTemplate as a bean in the configuration file and then pass it as a property of the DAO... it could work, yes, I just have to rework the configuration file. For the moment, I've configured the connection pool directly in the Hibernate session factory, I will have to move the data source out of it...

Similar Threads

  1. HibernateTemplate and transactions
    By Simon Brunning in forum Data
    Replies: 4
    Last Post: Mar 9th, 2009, 12:37 AM
  2. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  3. Inject HibernateTemplate into DAO
    By foamdino in forum Data
    Replies: 5
    Last Post: Jul 6th, 2005, 06:56 AM
  4. Injecting JdbcTemplate instead of just DataSource?
    By ArtVandelay in forum Container
    Replies: 4
    Last Post: Oct 20th, 2004, 10:22 PM
  5. Replies: 1
    Last Post: Oct 16th, 2004, 07:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •