views:

918

answers:

1

I am using Oracle 9 JDBC Thin Driver - the connection string I have used for standard JDBC was:

jdbcConn.connect("jdbc:oracle:thin:myDevDb/myDevDb@fooServer:1521:MYSIDNAME");

...just trying to get my head around using this kind of connection in Spring 2.5.

How do you wire up Spring to an Oracle connection - think it has something to do with an XML conifg file but not sure, there seems to be a couple of ways to do it.

Any help much appreciated...

LATEST EDIT

Thanks to those who have responded so far - but I need a bit of a "leg up" - on the part where you configure in the database connection string setup in your config, where do you put this info, and how? I have an existing Java web application - and I am trying to get to grips with how I 'shoehorn' Spring into my existing app.

+5  A: 

There are a few ways of doing this and it depends on what your environment is. If you're using Spring there's a fair chance you're deploying a Web application or you're otherwise in a J2EE environment. If this is the case (and arguably even if it isn't) you probably want to configure a DataSource.

This is a fairly minimal solution:

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
  </bean>

The above is using the Apache (Jakarta Commons) database connection pooling but your appserver probably has an alternative you may want to use instead. Also, different database vendors have their own data source implementations too (eg OracleDataSource and OracleXADataSource for Oracle).

Note the use of properties like jdbc.username. This is a typical configuration because database configurations typically vary between environment. You can activate a property configurator with something like:

<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesPlaceholderConfigurer">
  <property name="location" value="classpath:jdbc.properties"/>
</bean>

Now you probably want transactions too I would imagine. The easiest way is to use a platform transaction manager but, like with most things Spring, there are multiple ways of doing it.

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"/>
</bean>

After this you can use this bean directly or (arguably more common) you can use declarative transactions with AOP (annotations).

More on these subjects in the (superb) Spring reference documentation.

cletus
+1 - although would you not need a PropertiesPlaceholderConfigurer instead of PropertiesFactoryBean as you have shown?
toolkit
does the datasource info go into web.xml - I already have Restlet config info in there too - I take it I can a mix of config all in web.xml (sorry about the newbie nature of my question)
Vidar
usually you put your spring config in a separate xml file that you reference when you get the ApplictionContext. as far as i've seen the standard is application-config.xml
Alex Beardsley
@toolkit: yes I do. Thanks.
cletus