Spring Datasource and Database Schema

本小妞迷上赌 提交于 2019-12-01 03:24:16

问题


I am trying to declare a Spring datasource pointing to a DB2 database. Presently I am using a org.springframework.jdbc.datasource.DriverManagerDataSource to setup the connection but am not finding any way to specify the database schema in the database in the datasource bean. Could anyone help me on this?


回答1:


Problem is there is no standard way to set the schema, each database has a different mechanism.

A work around is to set the schema as part of the db url...

For db2 the url will look something like:

jdbc:db2://SERVER_NAME:PORT/DATABASE:currentSchema=SCHEMA_NAME;

hope that helps...

Special note: make sure you add the semicolon ; at the end of the URL, otherwise you will get errors saying URL is invalid. Also make sure nothing after last ; exists (not even spaces).




回答2:


There isn't a means to do this with the standard Spring namespace. Rob Harrop's response to a request to add the schema to the configuration:

In general, this kind of functionality should be pushed into the connection pool since there is no really elegant and performant way to do this via a decorator. The pool can set the schema once per connection it creates, whereas here you have to set it each time a connection is retrieved.

If you're desperate to set the proxy in your configuration, the submitter included some code for a proxy to allow the schema to be specified.




回答3:


If your connection uses the owner of the schema as the user then that connection will point to that particular schema. ie. If user user1 is the owner of the database schema named schema1 then if you connect to the database using user user1 then by default the connection will point to schema1.

We have used UserCredentialsDataSourceAdapter provided by spring to connect to different schemas based on the logged in user. This provides a datasource which is pointing to a particular schema based on the user. This uses a thread based login information. Each thread has to decide to which schema it has to connect and provide the user according to that.



来源:https://stackoverflow.com/questions/1248474/spring-datasource-and-database-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!