blob: 052bb2394bb1c8af00d7c872f87fa67daf84cfcb [file] [log] [blame]
/*
* Copyright (c) 2014 DataTorrent, Inc. ALL Rights Reserved.
*
* 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 com.datatorrent.lib.db.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.datatorrent.common.util.DTThrowable;
import com.datatorrent.lib.db.Connectable;
/**
* A {@link Connectable} that uses jdbc to connect to stores.
*
* @since 0.9.4
*/
public class JdbcStore implements Connectable
{
protected static final Logger logger = LoggerFactory.getLogger(JdbcStore.class);
@NotNull
private String databaseUrl;
@NotNull
private String databaseDriver;
private final Properties connectionProperties;
protected transient Connection connection = null;
/*
* Connection URL used to connect to the specific database.
*/
@NotNull
public String getDatabaseUrl()
{
return databaseUrl;
}
/*
* Connection URL used to connect to the specific database.
*/
public void setDatabaseUrl(@NotNull String databaseUrl)
{
this.databaseUrl = databaseUrl;
}
/*
* Driver used to connect to the specific database.
*/
@NotNull
public String getDatabaseDriver()
{
return databaseDriver;
}
/*
* Driver used to connect to the specific database.
*/
public void setDatabaseDriver(@NotNull String databaseDriver)
{
this.databaseDriver = databaseDriver;
}
public JdbcStore()
{
connectionProperties = new Properties();
}
public Connection getConnection()
{
return connection;
}
/**
* Sets the user name.
*
* @param userName user name
* @deprecated use {@link #setConnectionProperties(String)} instead.
*/
public void setUserName(String userName)
{
connectionProperties.put("user", userName);
}
/**
* Sets the password.
*
* @param password password
* @deprecated use {@link #setConnectionProperties(String)} instead.
*/
public void setPassword(String password)
{
connectionProperties.put("password", password);
}
/**
* Connection Properties for JDBC Connection.
* Sets the properties on the jdbc connection.
* @param connectionProps Comma separated list of properties. Property key and value are separated by colon.
* eg. user:xyz,password:ijk
*/
public void setConnectionProperties(String connectionProps)
{
String[] properties = Iterables.toArray(Splitter.on(CharMatcher.anyOf(":,")).omitEmptyStrings().trimResults().split(connectionProps), String.class);
for (int i = 0; i < properties.length; i += 2) {
if (i + 1 < properties.length) {
connectionProperties.put(properties[i], properties[i + 1]);
}
}
}
/**
* Connection Properties for JDBC Connection.
* Gets the properties on the jdbc connection.
*/
public Properties getConnectionProperties()
{
return connectionProperties;
}
/**
* Create connection with database using JDBC.
*/
@Override
public void connect()
{
try {
// This will load the JDBC driver, each DB has its own driver
Class.forName(databaseDriver).newInstance();
connection = DriverManager.getConnection(databaseUrl, connectionProperties);
logger.debug("JDBC connection Success");
}
catch (Throwable t) {
DTThrowable.rethrow(t);
}
}
/**
* Close JDBC connection.
*/
@Override
public void disconnect()
{
try {
connection.close();
}
catch (SQLException ex) {
throw new RuntimeException("closing database resource", ex);
}
}
@Override
public boolean isConnected()
{
try {
return !connection.isClosed();
}
catch (SQLException e) {
throw new RuntimeException("is isConnected", e);
}
}
}