Skip to content

Commit 559838b

Browse files
committed
Fix Hibernate tests to work with sqlproxy
1 parent f68f692 commit 559838b

File tree

6 files changed

+18
-33
lines changed

6 files changed

+18
-33
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/cockroachdb/examples-orms
33
go 1.13
44

55
require (
6-
github.com/cockroachdb/cockroach-go/v2 v2.2.2
6+
github.com/cockroachdb/cockroach-go/v2 v2.2.3
77
github.com/go-pg/pg/v10 v10.9.0
88
github.com/julienschmidt/httprouter v1.1.0
99
github.com/lib/pq v1.10.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
44
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
55
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
66
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
7-
github.com/cockroachdb/cockroach-go/v2 v2.2.2 h1:Xe6azwMWPr/HywnspK+REnDHVsod4WMwhgkAeIRC5Tk=
8-
github.com/cockroachdb/cockroach-go/v2 v2.2.2/go.mod h1:u3MiKYGupPPjkn3ozknpMUpxPaNLTFWAya419/zv6eI=
7+
github.com/cockroachdb/cockroach-go/v2 v2.2.3 h1:2881elKwTMrAWuSP2N/4PtU6XyqoyI55Fv3TSTD+Efo=
8+
github.com/cockroachdb/cockroach-go/v2 v2.2.3/go.mod h1:u3MiKYGupPPjkn3ozknpMUpxPaNLTFWAya419/zv6eI=
99
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
1010
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
1111
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=

java/hibernate/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repositories {
1212

1313
dependencies {
1414
// Necessary for Hibernate.
15-
implementation 'org.hibernate:hibernate-core:5.4.30.Final'
15+
implementation 'org.hibernate:hibernate-core:5.5.8.Final'
1616
implementation 'org.postgresql:postgresql:42.2.19'
1717

1818
// Necessary for web application.

java/hibernate/src/main/java/com/cockroachlabs/util/SessionUtil.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,26 @@ private SessionUtil(String dbAddr) {
3636
if (dbAddr != null) {
3737
// Most drivers expect the user in a connection to be specified like:
3838
// postgresql://<user>@host:port/db
39-
// but the PGJDBC expects the user as a parameter like:
40-
// postgresql://host:port/db?user=<user>
41-
Pattern p = Pattern.compile("postgresql://((\\w+)@).*");
39+
// but the PGJDBC driver expects the user as a parameter like:
40+
// postgresql://host:port/db
41+
// with the username and password passed as separate properties.
42+
Pattern p = Pattern.compile("postgresql://((\\w+)(:(\\w+))?@).*");
4243
Matcher m = p.matcher(dbAddr);
4344
if (m.matches()) {
4445
String userPart = m.group(1);
4546
String user = m.group(2);
47+
String password = m.group(4);
4648

47-
48-
String sep = "?";
49-
if (dbAddr.contains("?")) {
50-
sep = "&";
49+
dbAddr = dbAddr.replace(userPart, "");
50+
configuration.setProperty("hibernate.connection.user", user);
51+
if (password != null && !password.equals("")) {
52+
configuration.setProperty("hibernate.connection.password", password);
5153
}
52-
dbAddr = String.format("%s%suser=%s", dbAddr.replace(userPart, ""), sep, user);
5354
}
5455

56+
// The client cert must be in PKCS8 format for Java.
57+
dbAddr = dbAddr.replace("client.root.key", "client.root.key.pk8");
58+
5559
// Add the "jdbc:" prefix to the address and replace in configuration.
5660
dbAddr = "jdbc:" + dbAddr;
5761
configuration.setProperty("hibernate.connection.url", dbAddr);

java/hibernate/src/main/resources/hibernate.cfg.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<property name="connection.pool_size">16</property>
1717

1818
<!-- SQL dialect -->
19-
<property name="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
19+
<property name="dialect">org.hibernate.dialect.CockroachDB201Dialect</property>
2020

2121
<!-- Echo all executed SQL to stdout -->
2222
<property name="show_sql">true</property>

testing/main_test.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -435,26 +435,7 @@ func TestGOPG(t *testing.T) {
435435
}
436436

437437
func TestHibernate(t *testing.T) {
438-
testORMForAuthModesExcept(t,
439-
testInfo{language: "java", orm: "hibernate"},
440-
map[authMode]string{
441-
// Driver does not unescape the path correctly:
442-
// Caused by: java.io.FileNotFoundException:
443-
// %2Ftmp%2Fcockroach-testserver913095208%2Fcerts%2Fca.crt (No such file or directory)
444-
//
445-
// Furthermore, if we preprocess the query string via
446-
//
447-
// tc.dbURL.RawQuery, err = url.QueryUnescape(tc.dbURL.RawQuery)
448-
//
449-
// then we run into
450-
// https://github.com/dbeaver/dbeaver/issues/1835
451-
// because hibernate expects the key in DER format, but it is PEM.
452-
//authClientCert: "needs DER format and unescaped query string",
453-
// Doesn't seem to understand connection strings.
454-
// Caused by: java.net.UnknownHostException: root:hunter2@localhost
455-
//authPassword: "needs custom setup for password support",
456-
},
457-
)
438+
testORMForAuthModesExcept(t, testInfo{language: "java", orm: "hibernate"}, nothingSkipped())
458439
}
459440

460441
func TestSequelize(t *testing.T) {

0 commit comments

Comments
 (0)