public final class DataSourceFactory extends Object implements DataSource
This is the singleton class used to load the OpenNMS database configuration from the opennms-database.xml. This provides convenience methods to create database connections to the database configured in this default xml
Note: Users of this class should make sure the init() is called before calling any other method to ensure the config is loaded before accessing other convenience methods
Constructor and Description |
---|
DataSourceFactory() |
Modifier and Type | Method and Description |
---|---|
static void |
close()
close
|
Connection |
getConnection()
Return a new database connection to the database configured in the
opennms-database.xml.
|
Connection |
getConnection(String dsName)
getConnection
|
Connection |
getConnection(String username,
String password) |
static DataSource |
getDataSource()
Return the datasource configured for the database
|
static DataSource |
getDataSource(String dsName)
getDataSource
|
static DataSource |
getInstance()
Return the singleton instance of this factory.
|
static DataSource |
getInstance(String name)
getInstance
|
int |
getLoginTimeout()
getLoginTimeout
|
int |
getLoginTimeout(String dsName)
getLoginTimeout
|
PrintWriter |
getLogWriter()
getLogWriter
|
PrintWriter |
getLogWriter(String dsName)
getLogWriter
|
Logger |
getParentLogger() |
static void |
init()
Load the config from the default config file and create the singleton
instance of this factory.
|
static void |
init(String dsName)
init
|
void |
initialize()
initialize
|
boolean |
isWrapperFor(Class<?> iface)
isWrapperFor
|
static void |
setInstance(DataSource singleton)
setInstance
|
static void |
setInstance(String dsName,
DataSource singleton)
setInstance
|
void |
setLoginTimeout(int seconds) |
void |
setLoginTimeout(String dsName,
int seconds)
setLoginTimeout
|
void |
setLogWriter(PrintWriter out) |
void |
setLogWriter(String dsName,
PrintWriter out)
setLogWriter
|
<T> T |
unwrap(Class<T> iface)
unwrap
|
public static void init() throws IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException, ClassNotFoundException, PropertyVetoException, SQLException
IOException
- Thrown if the specified config file cannot be readorg.exolab.castor.xml.MarshalException
- Thrown if the file does not conform to the schema.org.exolab.castor.xml.ValidationException
- Thrown if the contents do not match the required schema.SQLException
- if any.PropertyVetoException
- if any.IOException
- if any.org.exolab.castor.xml.MarshalException
- if any.org.exolab.castor.xml.ValidationException
- if any.ClassNotFoundException
- if any.public static void init(String dsName) throws IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException, ClassNotFoundException, PropertyVetoException, SQLException
init
dsName
- a String
object.IOException
- if any.org.exolab.castor.xml.MarshalException
- if any.org.exolab.castor.xml.ValidationException
- if any.ClassNotFoundException
- if any.PropertyVetoException
- if any.SQLException
- if any.public static DataSource getInstance()
Return the singleton instance of this factory. This is the instance of
the factory that was last created when the
init
or
reload
method was invoked. The instance will not change
unless a reload
method is invoked.
IllegalStateException
- Thrown if the factory has not yet been initialized.public static DataSource getInstance(String name)
getInstance
name
- a String
object.DataSource
object.public Connection getConnection() throws SQLException
close
method.getConnection
in interface DataSource
SQLException
- Thrown if there is an error opening the connection to the
database.public Connection getConnection(String dsName) throws SQLException
getConnection
dsName
- a String
object.Connection
object.SQLException
- if any.public static void setInstance(DataSource singleton)
setInstance
singleton
- a DataSource
object.public static void setInstance(String dsName, DataSource singleton)
setInstance
dsName
- a String
object.singleton
- a DataSource
object.public static DataSource getDataSource()
public static DataSource getDataSource(String dsName)
getDataSource
dsName
- a String
object.DataSource
object.public Connection getConnection(String username, String password) throws SQLException
getConnection
in interface DataSource
SQLException
public PrintWriter getLogWriter() throws SQLException
getLogWriter
getLogWriter
in interface CommonDataSource
PrintWriter
object.SQLException
- if any.public PrintWriter getLogWriter(String dsName) throws SQLException
getLogWriter
dsName
- a String
object.PrintWriter
object.SQLException
- if any.public void setLogWriter(PrintWriter out) throws SQLException
setLogWriter
in interface CommonDataSource
SQLException
public void setLogWriter(String dsName, PrintWriter out) throws SQLException
setLogWriter
dsName
- a String
object.out
- a PrintWriter
object.SQLException
- if any.public void setLoginTimeout(int seconds) throws SQLException
setLoginTimeout
in interface CommonDataSource
SQLException
public void setLoginTimeout(String dsName, int seconds) throws SQLException
setLoginTimeout
dsName
- a String
object.seconds
- a int.SQLException
- if any.public int getLoginTimeout() throws SQLException
getLoginTimeout
getLoginTimeout
in interface CommonDataSource
SQLException
- if any.public int getLoginTimeout(String dsName) throws SQLException
getLoginTimeout
dsName
- a String
object.SQLException
- if any.public void initialize() throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException, IOException, ClassNotFoundException
initialize
org.exolab.castor.xml.MarshalException
- if any.org.exolab.castor.xml.ValidationException
- if any.IOException
- if any.ClassNotFoundException
- if any.public Logger getParentLogger() throws SQLFeatureNotSupportedException
getParentLogger
in interface CommonDataSource
SQLFeatureNotSupportedException
public static void close() throws SQLException
close
SQLException
- if any.public <T> T unwrap(Class<T> iface) throws SQLException
unwrap
unwrap
in interface Wrapper
T
- a T object.iface
- a Class
object.SQLException
- if any.public boolean isWrapperFor(Class<?> iface) throws SQLException
isWrapperFor
isWrapperFor
in interface Wrapper
iface
- a Class
object.SQLException
- if any.Copyright © 2015. All Rights Reserved.