Spring 中的 Oracle 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/520041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:45:42  来源:igfitidea点击:

Oracle connections in Spring

javaoraclespringconnection-string

提问by Vidar

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

我正在使用 Oracle 9 JDBC Thin Driver - 我用于标准 JDBC 的连接字符串是:

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.

...只是想在 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.

您如何将 Spring 连接到 Oracle 连接 - 认为它与 XML conifg 文件有关,但不确定,似乎有几种方法可以做到。

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.

感谢到目前为止已经做出回应的人 - 但我需要一点“支持” - 在您在配置中的数据库连接字符串设置中进行配置的部分,您将这些信息放在哪里,以及如何放置?我有一个现有的 Java Web 应用程序 - 我正试图掌握如何将我“鞋拔”到我现有的应用程序中。

回答by cletus

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.

有几种方法可以做到这一点,这取决于您的环境。如果您正在使用 Spring,那么您很可能正在部署 Web 应用程序,或者您处于 J2EE 环境中。如果是这种情况(并且可以说即使不是),您可能想要配置一个数据源。

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

以上是使用 Apache (Jakarta Commons) 数据库连接池,但您的应用服务器可能有一个您可能想要使用的替代方案。此外,不同的数据库供应商也有自己的数据源实现(例如 OracleDataSource 和 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:

请注意 jdbc.username 等属性的使用。这是典型的配置,因为数据库配置通常因环境而异。您可以使用以下内容激活属性配置器:

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

现在你可能也想要交易,我可以想象。最简单的方法是使用平台事务管理器,但与 Spring 的大多数事情一样,有多种方法可以做到。

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

在此之后,您可以直接使用这个 bean,或者(可以说更常见)您可以使用带有 AOP(注释)的声明性事务。

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

(极好的)Spring 参考文档中有关这些主题的更多信息