Skip to content

Commit c334166

Browse files
committed
Support JDBC Thin-style service name syntax
Oracle enhanced has supported Oracle SID syntax. Since Oracle service name is mandatory for Oracle pluggable database, it's time to support service name connection syntax. - SID syntax ``` jdbc:oracle:thin:@myHost:1521:orcl ``` Refer https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html - Service name syntax ``` jdbc:oracle:thin:@//127.0.0.1:1521/ORCLPDB1 ``` Refer "8.2.4 Thin-style Service Name Syntax" https://docs.oracle.com/en/database/oracle/oracle-database/18/jjdbc/data-sources-and-URLs.html#GUID-EF07727C-50AB-4DCE-8EDC-57F0927FF61A
1 parent f834097 commit c334166

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ def new_connection(config)
115115
else
116116
unless database.match?(/^(\:|\/)/)
117117
# assume database is a SID if no colon or slash are supplied (backward-compatibility)
118-
database = ":#{database}"
118+
database = "/#{database}"
119119
end
120-
url = config[:url] || "jdbc:oracle:thin:@#{host || 'localhost'}:#{port || 1521}#{database}"
120+
url = config[:url] || "jdbc:oracle:thin:@//#{host || 'localhost'}:#{port || 1521}#{database}"
121121
end
122122

123123
prefetch_rows = config[:prefetch_rows] || 100

spec/active_record/connection_adapters/oracle_enhanced/connection_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
ActiveRecord::Base.establish_connection(SYSTEM_CONNECTION_PARAMS.merge(cursor_sharing: :exact))
4141
expect(ActiveRecord::Base.connection.select_value("select value from v$parameter where name = 'cursor_sharing'")).to eq("EXACT")
4242
end
43+
44+
it "should connect to database using service_name" do
45+
ActiveRecord::Base.establish_connection(SERVICE_NAME_CONNECTION_PARAMS)
46+
expect(ActiveRecord::Base.connection).not_to be_nil
47+
expect(ActiveRecord::Base.connection.class).to eq(ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter)
48+
end
4349
end
4450

4551
describe "OracleEnhancedConnection" do

spec/spec_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ def dump_table_schema(table, connection = ActiveRecord::Base.connection)
185185
password: DATABASE_SYS_PASSWORD
186186
}
187187

188+
SERVICE_NAME_CONNECTION_PARAMS = {
189+
adapter: "oracle_enhanced",
190+
database: "/#{DATABASE_NAME}",
191+
host: DATABASE_HOST,
192+
port: DATABASE_PORT,
193+
username: DATABASE_USER,
194+
password: DATABASE_PASSWORD
195+
}
196+
188197
DATABASE_NON_DEFAULT_TABLESPACE = config["database"]["non_default_tablespace"] || ENV["DATABASE_NON_DEFAULT_TABLESPACE"] || "SYSTEM"
189198

190199
# set default time zone in TZ environment variable

0 commit comments

Comments
 (0)