Announcement Announcement Module
Collapse
No announcement yet.
HibernateTemplate + JdbcTemplate + HibernateTransactionMgr Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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...

    Comment


    • #3
      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.

      Comment


      • #4
        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).

        Comment


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

          Comment


          • #6
            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...

            Comment

            Working...
            X