[Major] Refactored DB package: Now use DataSource classes
- this allows to inject a connection pool
This commit is contained in:
parent
556981777c
commit
dd1489772d
|
@ -16,7 +16,6 @@
|
|||
package ch.eitchnet.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
@ -24,6 +23,8 @@ import java.text.MessageFormat;
|
|||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -33,13 +34,13 @@ import org.slf4j.LoggerFactory;
|
|||
public class DbConnectionCheck {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DbConnectionCheck.class);
|
||||
private Map<String, DbConnectionInfo> connetionInfoMap;
|
||||
private Map<String, DataSource> dsMap;
|
||||
|
||||
/**
|
||||
* @param connetionInfoMap
|
||||
* @param dsMap
|
||||
*/
|
||||
public DbConnectionCheck(Map<String, DbConnectionInfo> connetionInfoMap) {
|
||||
this.connetionInfoMap = connetionInfoMap;
|
||||
public DbConnectionCheck(Map<String, DataSource> dsMap) {
|
||||
this.dsMap = dsMap;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,17 +49,12 @@ public class DbConnectionCheck {
|
|||
* @throws DbException
|
||||
*/
|
||||
public void checkConnections() throws DbException {
|
||||
Collection<DbConnectionInfo> values = this.connetionInfoMap.values();
|
||||
for (DbConnectionInfo connectionInfo : values) {
|
||||
Collection<DataSource> values = this.dsMap.values();
|
||||
for (DataSource ds : values) {
|
||||
|
||||
String url = connectionInfo.getUrl();
|
||||
String username = connectionInfo.getUsername();
|
||||
String password = connectionInfo.getPassword();
|
||||
logger.info("Checking connection " + ds);
|
||||
|
||||
logger.info("Checking connection " + username + "@" + url);
|
||||
|
||||
try (Connection con = DriverManager.getConnection(url, username, password);
|
||||
Statement st = con.createStatement();) {
|
||||
try (Connection con = ds.getConnection(); Statement st = con.createStatement();) {
|
||||
|
||||
try (ResultSet rs = st.executeQuery("select version()")) { //$NON-NLS-1$
|
||||
if (rs.next()) {
|
||||
|
@ -67,8 +63,8 @@ public class DbConnectionCheck {
|
|||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
String msg = "Failed to open DB connection to URL {0} due to: {1}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, url, e.getMessage());
|
||||
String msg = "Failed to open DB connection to {0} due to: {1}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, ds, e.getMessage());
|
||||
throw new DbException(msg, e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,132 +0,0 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ch.eitchnet.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.text.MessageFormat;
|
||||
|
||||
import ch.eitchnet.utils.dbc.DBC;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class DbConnectionInfo {
|
||||
|
||||
private String realm;
|
||||
private String url;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public DbConnectionInfo(String realm, String url) {
|
||||
DBC.PRE.assertNotEmpty("Realm must be set!", realm); //$NON-NLS-1$
|
||||
DBC.PRE.assertNotEmpty("Url must be set!", url); //$NON-NLS-1$
|
||||
this.realm = realm;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the realm
|
||||
*/
|
||||
public String getRealm() {
|
||||
return this.realm;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param realm
|
||||
* the realm to set
|
||||
*/
|
||||
public void setRealm(String realm) {
|
||||
this.realm = realm;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the url
|
||||
*/
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url
|
||||
* the url to set
|
||||
*/
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the username
|
||||
*/
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username
|
||||
* the username to set
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the password
|
||||
*/
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param password
|
||||
* the password to set
|
||||
*/
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("DbConnectionInfo [realm=");
|
||||
builder.append(this.realm);
|
||||
builder.append(", url=");
|
||||
builder.append(this.url);
|
||||
builder.append(", username=");
|
||||
builder.append(this.username);
|
||||
builder.append(", password=***");
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a {@link Connection}
|
||||
*
|
||||
* @throws DbException
|
||||
*/
|
||||
public Connection openConnection() throws DbException {
|
||||
try {
|
||||
Connection connection = DriverManager.getConnection(this.url, this.username, this.password);
|
||||
connection.setAutoCommit(false);
|
||||
return connection;
|
||||
} catch (SQLException e) {
|
||||
String msg = MessageFormat.format("Failed to get a connection for {0} due to {1}", this, e.getMessage()); //$NON-NLS-1$
|
||||
throw new DbException(msg, e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ch.eitchnet.db;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*/
|
||||
public interface DbDataSourceBuilder {
|
||||
|
||||
public DataSource build(String realm, String url, String username, String password, Properties properties);
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ch.eitchnet.db;
|
||||
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.text.MessageFormat;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class DbDriverLoader {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DbDriverLoader.class);
|
||||
|
||||
public static void loadDriverForConnection(DbConnectionInfo connectionInfo) throws DbException {
|
||||
Driver driver;
|
||||
try {
|
||||
driver = DriverManager.getDriver(connectionInfo.getUrl());
|
||||
} catch (SQLException e) {
|
||||
String msg = "Failed to load DB driver for URL {0} due to: {1}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, connectionInfo.getUrl(), e.getMessage());
|
||||
throw new DbException(msg, e);
|
||||
}
|
||||
|
||||
String compliant = driver.jdbcCompliant() ? "" : "non"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String msg = "Realm {0}: Using {1} JDBC compliant Driver {2}.{3}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, connectionInfo.getRealm(), compliant, driver.getMajorVersion(),
|
||||
driver.getMinorVersion());
|
||||
logger.info(msg);
|
||||
}
|
||||
}
|
|
@ -21,7 +21,6 @@ import static ch.eitchnet.db.DbConstants.RESOURCE_DB_VERSION;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
@ -29,8 +28,11 @@ import java.sql.Statement;
|
|||
import java.text.MessageFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -79,34 +81,34 @@ public class DbSchemaVersionCheck {
|
|||
return this.dbMigrationStates;
|
||||
}
|
||||
|
||||
public void checkSchemaVersion(Map<String, DbConnectionInfo> connectionInfoMap) throws DbException {
|
||||
for (DbConnectionInfo connectionInfo : connectionInfoMap.values()) {
|
||||
DbMigrationState dbMigrationState = checkSchemaVersion(connectionInfo);
|
||||
dbMigrationStates.put(connectionInfo.getRealm(), dbMigrationState);
|
||||
public void checkSchemaVersion(Map<String, DataSource> dsMap) throws DbException {
|
||||
for (Entry<String, DataSource> entry : dsMap.entrySet()) {
|
||||
String realm = entry.getKey();
|
||||
DataSource ds = entry.getValue();
|
||||
DbMigrationState dbMigrationState = checkSchemaVersion(realm, ds);
|
||||
dbMigrationStates.put(realm, dbMigrationState);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the schema existed or was only migrated, false if the schema was created
|
||||
*
|
||||
* @param ds
|
||||
* @param realm2
|
||||
*
|
||||
* @param connectionInfo
|
||||
*
|
||||
* @return true if the schema existed or was only migrated, false if the schema was created
|
||||
*
|
||||
* @throws DbException
|
||||
*/
|
||||
public DbMigrationState checkSchemaVersion(DbConnectionInfo connectionInfo) throws DbException {
|
||||
String realm = connectionInfo.getRealm();
|
||||
String url = connectionInfo.getUrl();
|
||||
String username = connectionInfo.getUsername();
|
||||
String password = connectionInfo.getPassword();
|
||||
public DbMigrationState checkSchemaVersion(String realm, DataSource ds) throws DbException {
|
||||
|
||||
logger.info(MessageFormat.format("[{0}:{1}] Checking Schema version for: {2}@{3}", this.app, realm, username,
|
||||
url));
|
||||
logger.info(MessageFormat.format("[{0}:{1}] Checking Schema version for: {2}", this.app, realm, ds));
|
||||
|
||||
Version expectedDbVersion = getExpectedDbVersion(this.app, this.ctxClass);
|
||||
|
||||
try (Connection con = DriverManager.getConnection(url, username, password)) {
|
||||
try (Connection con = ds.getConnection()) {
|
||||
|
||||
// get current version
|
||||
Version currentVersion = getCurrentVersion(con, this.app);
|
||||
|
@ -127,11 +129,12 @@ public class DbSchemaVersionCheck {
|
|||
break;
|
||||
}
|
||||
|
||||
con.commit();
|
||||
return migrationType;
|
||||
|
||||
} catch (SQLException e) {
|
||||
String msg = "Failed to open DB connection to URL {0} due to: {1}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, url, e.getMessage());
|
||||
String msg = "Failed to open DB connection to {0} due to: {1}"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, ds, e.getMessage());
|
||||
throw new DbException(msg, e);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue