views:

1863

answers:

3

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?

+3  A: 

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()

Kevin
Our session factory was created using AnnotationSessionFactoryBean initialized with hibernate properties. We didn't create and initialize a DataSource. I can't pull the DataSource from the SessionFactoryBean because I don't get to keep a reference to it: <bean id="hibernateSessionFactory" class="com.monsanto.tps.pipelinemanagement.server.hibernate.SessionFactoryBean"> <property name="hibernateProperties" ref="hibernateProperties"/>...hibernateSessionFactory is a SessionFactory. How would I get a reference to the SessionFactoryBean?
Bryan Young
What is your custom SessionFactoryBean doing to build the SessionFactory?In the worst case, are you at least providing the JDBC driver name and connection URL in the hibernate properties? If so, you should be able to instantiate a DataSource from that. Better yet, build a DataSource and share it between the SessionFactoryBean and the JDBCTemplate
Kevin
The custom FactoryBean is unreleated to this issue. It's a subclass of AnnotationSessionFactoryBean. I don't wan't to introduce another datasource for maintaince reasons, and extracting a datasource from our hibernate configuration seems like a lot of work for what I need. In retrospect, it should have been created that way in the first place, but the app was essentially converted to Spring after the fact. I'm just going to use hibernate's createSQLQuery() rather than JdbcTemplate. Thanks for the help.
Bryan Young
A: 

"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?

Mark Serrano
I guess "a lot of work" is a relative term. In my case, I would need to create a c3p0-based data source and translate all our settings from the original properties file to this one. Property names change on some settings between Hibernate and c3p0, so I would need to verify them all. It would probably take about an hour to do and verify, but considering the gain is simply use of JDBCTemplate in one dao, it's not worth the time.AnnotationSessionFactoryBean subclasses LocalSessionFactoryBean.The reference to the session factory doesn't help me initialize JDBCTemplate. I need the datasource.
Bryan Young
A: 

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

Nicholas White