-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
BasicConnectionCreator.java
131 lines (113 loc) · 4.09 KB
/
BasicConnectionCreator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.engine.jdbc.connections.internal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.exception.JDBCConnectionException;
import org.hibernate.exception.internal.SQLStateConversionDelegate;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.internal.util.ValueHolder;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* Template (as in template pattern) support for ConnectionCreator implementors.
*
* @author Steve Ebersole
*/
public abstract class BasicConnectionCreator implements ConnectionCreator {
private final ServiceRegistryImplementor serviceRegistry;
private final String url;
private final Properties connectionProps;
private final boolean autoCommit;
private final Integer isolation;
public BasicConnectionCreator(
ServiceRegistryImplementor serviceRegistry,
String url,
Properties connectionProps,
boolean autocommit,
Integer isolation) {
this.serviceRegistry = serviceRegistry;
this.url = url;
this.connectionProps = connectionProps;
this.autoCommit = autocommit;
this.isolation = isolation;
}
@Override
public String getUrl() {
return url;
}
@Override
public Connection createConnection() {
final Connection conn = makeConnection( url, connectionProps );
if ( conn == null ) {
throw new HibernateException( "Unable to make JDBC Connection [" + url + "]" );
}
try {
if ( isolation != null ) {
conn.setTransactionIsolation( isolation );
}
}
catch (SQLException e) {
throw convertSqlException( "Unable to set transaction isolation (" + isolation + ")", e );
}
try {
if ( conn.getAutoCommit() != autoCommit ) {
conn.setAutoCommit( autoCommit );
}
}
catch (SQLException e) {
throw convertSqlException( "Unable to set auto-commit (" + autoCommit + ")", e );
}
return conn;
}
private ValueHolder<SQLExceptionConversionDelegate> simpleConverterAccess =
new ValueHolder<>( () -> new SQLExceptionConversionDelegate() {
private final SQLStateConversionDelegate sqlStateDelegate = new SQLStateConversionDelegate(
() -> {
// this should never happen...
throw new HibernateException( "Unexpected call to ConversionContext.getViolatedConstraintNameExtractor" );
}
);
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
JDBCException exception = sqlStateDelegate.convert( sqlException, message, sql );
if ( exception == null ) {
// assume this is either a set-up problem or a problem connecting, which we will
// categorize the same here.
exception = new JDBCConnectionException( message, sqlException, sql );
}
return exception;
}
}
);
protected JDBCException convertSqlException(String message, SQLException e) {
try {
// if JdbcServices#getSqlExceptionHelper is available, use it...
final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
if ( jdbcServices != null && jdbcServices.getSqlExceptionHelper() != null ) {
return jdbcServices.getSqlExceptionHelper().convert( e, message, null );
}
}
catch (ServiceException se) {
//swallow it because we're in the process of initializing JdbcServices
}
// likely we are still in the process of initializing the ServiceRegistry, so use the simplified
// SQLException conversion
return simpleConverterAccess.getValue().convert( e, message, null );
}
protected abstract Connection makeConnection(String url, Properties connectionProps);
/**
* Exposed for testing purposes only.
*/
public Properties getConnectionProperties() {
return new Properties( connectionProps );
}
}