Skip to content

Commit 4e3d17c

Browse files
committed
HHH-8453 - Investigate improving DriverManager-based connection pooling
1 parent 9671a4a commit 4e3d17c

File tree

8 files changed

+387
-141
lines changed

8 files changed

+387
-141
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.engine.jdbc.connections.internal;
25+
26+
import java.sql.Connection;
27+
import java.sql.SQLException;
28+
import java.util.Properties;
29+
30+
import org.hibernate.JDBCException;
31+
import org.hibernate.engine.jdbc.spi.JdbcServices;
32+
import org.hibernate.service.spi.ServiceRegistryImplementor;
33+
34+
/**
35+
* Template (as in template pattern) support for ConnectionCreator implementors.
36+
*
37+
* @author Steve Ebersole
38+
*/
39+
public abstract class BasicConnectionCreator implements ConnectionCreator {
40+
private final ServiceRegistryImplementor serviceRegistry;
41+
42+
private final String url;
43+
private final Properties connectionProps;
44+
45+
private final boolean autoCommit;
46+
private final Integer isolation;
47+
48+
public BasicConnectionCreator(
49+
ServiceRegistryImplementor serviceRegistry,
50+
String url,
51+
Properties connectionProps,
52+
boolean autocommit,
53+
Integer isolation) {
54+
this.serviceRegistry = serviceRegistry;
55+
this.url = url;
56+
this.connectionProps = connectionProps;
57+
this.autoCommit = autocommit;
58+
this.isolation = isolation;
59+
}
60+
61+
@Override
62+
public String getUrl() {
63+
return url;
64+
}
65+
66+
@Override
67+
public Connection createConnection() {
68+
final Connection conn = makeConnection( url, connectionProps );
69+
70+
try {
71+
if ( isolation != null ) {
72+
conn.setTransactionIsolation( isolation );
73+
}
74+
}
75+
catch (SQLException e) {
76+
throw convertSqlException( "Unable to set transaction isolation (" + isolation + ")", e );
77+
}
78+
79+
try {
80+
if ( conn.getAutoCommit() != autoCommit ) {
81+
conn.setAutoCommit( autoCommit );
82+
}
83+
}
84+
catch (SQLException e) {
85+
throw convertSqlException( "Unable to set auto-commit (" + autoCommit + ")", e );
86+
}
87+
88+
return conn;
89+
}
90+
91+
protected JDBCException convertSqlException(String message, SQLException e) {
92+
return serviceRegistry.getService( JdbcServices.class ).getSqlExceptionHelper().convert( e, message, null );
93+
}
94+
95+
protected abstract Connection makeConnection(String url, Properties connectionProps);
96+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.engine.jdbc.connections.internal;
25+
26+
import java.sql.Connection;
27+
28+
/**
29+
* Contract for creating JDBC Connections on demand
30+
*
31+
* @author Steve Ebersole
32+
*/
33+
interface ConnectionCreator {
34+
/**
35+
* Obtain the URL to which this creator connects. Intended just for informational (logging) purposes.
36+
*
37+
* @return The connection URL.
38+
*/
39+
public String getUrl();
40+
41+
/**
42+
* Create a Connection
43+
*
44+
* @return The created Connection
45+
*/
46+
public Connection createConnection();
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.engine.jdbc.connections.internal;
25+
26+
import java.sql.Driver;
27+
import java.util.Properties;
28+
29+
import org.hibernate.service.spi.ServiceRegistryImplementor;
30+
31+
/**
32+
* A builder for ConnectionCreator instances
33+
*
34+
* @author Steve Ebersole
35+
*/
36+
public class ConnectionCreatorBuilder {
37+
private final ServiceRegistryImplementor serviceRegistry;
38+
39+
private Driver driver;
40+
41+
private String url;
42+
private Properties connectionProps;
43+
44+
private boolean autoCommit;
45+
private Integer isolation;
46+
47+
public ConnectionCreatorBuilder(ServiceRegistryImplementor serviceRegistry) {
48+
this.serviceRegistry = serviceRegistry;
49+
}
50+
51+
public void setDriver(Driver driver) {
52+
this.driver = driver;
53+
}
54+
55+
public void setUrl(String url) {
56+
this.url = url;
57+
}
58+
59+
public void setConnectionProps(Properties connectionProps) {
60+
this.connectionProps = connectionProps;
61+
}
62+
63+
public void setAutoCommit(boolean autoCommit) {
64+
this.autoCommit = autoCommit;
65+
}
66+
67+
public void setIsolation(Integer isolation) {
68+
this.isolation = isolation;
69+
}
70+
71+
public ConnectionCreator build() {
72+
if ( driver == null ) {
73+
return new DriverManagerConnectionCreator( serviceRegistry, url, connectionProps, autoCommit, isolation );
74+
}
75+
else {
76+
return new DriverConnectionCreator( driver, serviceRegistry, url, connectionProps, autoCommit, isolation );
77+
}
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.engine.jdbc.connections.internal;
25+
26+
import java.sql.Connection;
27+
import java.sql.Driver;
28+
import java.sql.SQLException;
29+
import java.util.Properties;
30+
31+
import org.hibernate.service.spi.ServiceRegistryImplementor;
32+
33+
/**
34+
* A specialized ConnectionCreator which uses {@link Driver#connect} to generate Connections
35+
*
36+
* @author Steve Ebersole
37+
*/
38+
public class DriverConnectionCreator extends BasicConnectionCreator {
39+
private final Driver driver;
40+
41+
public DriverConnectionCreator(
42+
Driver driver,
43+
ServiceRegistryImplementor serviceRegistry,
44+
String url,
45+
Properties connectionProps,
46+
Boolean autocommit,
47+
Integer isolation) {
48+
super( serviceRegistry, url, connectionProps, autocommit, isolation );
49+
this.driver = driver;
50+
}
51+
52+
@Override
53+
protected Connection makeConnection(String url, Properties connectionProps) {
54+
try {
55+
return driver.connect( url, connectionProps );
56+
}
57+
catch (SQLException e) {
58+
throw convertSqlException( "Error calling Driver#connect", e );
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.engine.jdbc.connections.internal;
25+
26+
import java.sql.Connection;
27+
import java.sql.DriverManager;
28+
import java.sql.SQLException;
29+
import java.util.Properties;
30+
31+
import org.hibernate.service.spi.ServiceRegistryImplementor;
32+
33+
/**
34+
* A specialized ConnectionCreator which uses {@link DriverManager#getConnection} to generate Connections
35+
*
36+
* @author Steve Ebersole
37+
*/
38+
public class DriverManagerConnectionCreator extends BasicConnectionCreator {
39+
public DriverManagerConnectionCreator(
40+
ServiceRegistryImplementor serviceRegistry,
41+
String url,
42+
Properties connectionProps,
43+
Boolean autocommit,
44+
Integer isolation) {
45+
super( serviceRegistry, url, connectionProps, autocommit, isolation );
46+
}
47+
48+
@Override
49+
protected Connection makeConnection(String url, Properties connectionProps) {
50+
try {
51+
return DriverManager.getConnection( url, connectionProps );
52+
}
53+
catch (SQLException e) {
54+
throw convertSqlException( "Error calling DriverManager#getConnection", e );
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)