Oracle连接URL中的默认架构

net*_*tic 40 oracle connection-string jdbc

我想在中设置默认数据库架构 Oracle Connection URL

jdbc:oracle:thin:@<server>:<port1521>:<sid>
Run Code Online (Sandbox Code Playgroud)

我的示例SQL语句:

select monkey_name from animals.monkey
Run Code Online (Sandbox Code Playgroud)

我需要查询没有模式前缀的数据库,animals.即当我运行此语句时

select monkey_name from monkey
Run Code Online (Sandbox Code Playgroud)

animals默认使用模式.

我需要在上面的连接URL中指定什么才能获得这样的效果?

谢谢.

Gar*_*ers 34

您不能在连接URL中放置任何内容.

在Oracle中,每个用户都有自己的架构(即使不包含任何对象),这也是它们的默认架构.登录/连接后,他们可以使用a更改其默认架构

ALTER SESSION SET CURRENT_SCHEMA=animals
Run Code Online (Sandbox Code Playgroud)

所以你需要在连接后做额外的声明.可以在用户和/或数据库上有一个登录触发器,当他们登录时会运行它.我个人更喜欢在应用程序连接时使用显式语句.


Dan*_*art 13

如果您使用C3PO,则可以在检查连接时执行此操作.

作为属性:

c3p0.preferredTestQuery=alter session set current_schema=animals
c3p0.testConnectionOnCheckout=true
Run Code Online (Sandbox Code Playgroud)

作为Java代码:

ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setPreferredTestQuery("alter session set current_schema=animals");
dataSource.setTestConnectionOnCheckout(true);
Run Code Online (Sandbox Code Playgroud)

下行是每次从池中取出连接时都会发生这种情况.

如果您自己使用JDBC连接,您可以这样做:

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = getConnection("jdbc:oracle:thin:@//server:1521/instance",   "username", "password");
connection.createStatement().execute("alter session set current_schema=animals"));
Run Code Online (Sandbox Code Playgroud)


Ren*_*ene 12

使用同义词怎么样?

create synonym monkey for animals.monkey;

select monkey_name from monkey
Run Code Online (Sandbox Code Playgroud)

  • +此解决方案非常常用且运行良好. (4认同)
  • 这通常是改变会话的更好选择,因为它将问题从行为方面转移到结构,这更加健壮. (2认同)