Question

We have a Spring/Hibernate app and would like to add a small amount of JDBC for reasons involving performance and development time. I can make this dao subclass HibernateDaoSupport and use the session's connection to perform my JDBC, but I'd rather use JdbcTemplate. JdbcTemplate, however is initialized using a java.sql.Datasource. How can I use my existing Hibernate SessionFactory to initialize it?

Was it helpful?

Solution

Aren't you required to provide a DataSource to the SessionFactory implementation? Why don't you wire that in to the JDBC Template?

Which SessionFactory implementation are you using? If you're using the Spring implementations, see AbstractSessionFactoryBean.html#getDataSource()

OTHER TIPS

You can always use a hibernate session's doWork method - this gives you a java.sql.Connection. You can use this connection to construct a construct a SingleConnectionDataSource (note: the second argument should always be true as you don't want to close the underlying connection) and pass this datasource to your JDBCTemplate...

"extracting a datasource from our hibernate configuration seems like a lot of work for what I need"

I don't see why it would take that much work. It's just a matter of creating, cut-and-copying a couple of tags and properties.

For example:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource">
          <ref bean="dataSource"/>    
      </property>
...
</bean>

"Which SessionFactory implementation are you using? If you're using the Spring implementations, see AbstractSessionFactoryBean.html#getDataSource()"

Apparently, getDataSource() is only available for Spring 2.5. Here's the reference: Click here

Spring 2.0 doesn't have the getDataSource(). Here's the reference: Click here

Our session factory was created using AnnotationSessionFactoryBean initialized with hibernate properties ... hibernateSessionFactory is a SessionFactory. How would I get a reference to the SessionFactoryBean?

I'm wondering why you used a SessionFactory instead of a LocalSessionFactoryBean which is a subclass of AnnotationSessionFactoryBean?

Isn't the line bean id="hibernateSessionFactory" references the SessionFactoryBean already?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top