Package org.h2.jdbcx
Class JdbcDataSource
- java.lang.Object
-
- org.h2.message.TraceObject
-
- org.h2.jdbcx.JdbcDataSource
-
- All Implemented Interfaces:
java.io.Serializable,java.sql.Wrapper,javax.naming.Referenceable,javax.sql.CommonDataSource,javax.sql.ConnectionPoolDataSource,javax.sql.DataSource,javax.sql.XADataSource,JdbcDataSourceBackwardsCompat
public final class JdbcDataSource extends TraceObject implements javax.sql.XADataSource, javax.sql.DataSource, javax.sql.ConnectionPoolDataSource, java.io.Serializable, javax.naming.Referenceable, JdbcDataSourceBackwardsCompat
A data source for H2 database connections. It is a factory for XAConnection and Connection objects. This class is usually registered in a JNDI naming service. To create a data source object and register it with a JNDI service, use the following code:import org.h2.jdbcx.JdbcDataSource; import javax.naming.Context; import javax.naming.InitialContext; JdbcDataSource ds = new JdbcDataSource(); ds.setURL("jdbc:h2:˜/test"); ds.setUser("sa"); ds.setPassword("sa"); Context ctx = new InitialContext(); ctx.bind("jdbc/dsName", ds);To use a data source that is already registered, use the following code:import java.sql.Connection; import javax.sql.DataSource; import javax.naming.Context; import javax.naming.InitialContext; Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("jdbc/dsName"); Connection conn = ds.getConnection();In this example the user name and password are serialized as well; this may be a security problem in some cases.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Stringdescriptionprivate JdbcDataSourceFactoryfactoryprivate intloginTimeoutprivate java.io.PrintWriterlogWriterprivate char[]passwordCharsprivate static longserialVersionUIDprivate java.lang.Stringurlprivate java.lang.StringuserName-
Fields inherited from class org.h2.message.TraceObject
ARRAY, BLOB, CALLABLE_STATEMENT, CLOB, CONNECTION, DATA_SOURCE, DATABASE_META_DATA, PARAMETER_META_DATA, PREPARED_STATEMENT, RESULT_SET, RESULT_SET_META_DATA, SAVEPOINT, SQLXML, STATEMENT, trace, XA_DATA_SOURCE, XID
-
-
Constructor Summary
Constructors Constructor Description JdbcDataSource()The public constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.lang.StringconvertToString(char[] a)java.sql.ConnectiongetConnection()Open a new connection using the current URL, user name and password.java.sql.ConnectiongetConnection(java.lang.String user, java.lang.String password)Open a new connection using the current URL and the specified user name and password.java.lang.StringgetDescription()Get the current description.intgetLoginTimeout()Get the login timeout in seconds, 0 meaning no timeout.java.io.PrintWritergetLogWriter()Get the current log writer for this object.java.util.logging.LoggergetParentLogger()[Not supported]java.lang.StringgetPassword()Get the current password.javax.sql.PooledConnectiongetPooledConnection()Open a new pooled connection using the current URL, user name and password.javax.sql.PooledConnectiongetPooledConnection(java.lang.String user, java.lang.String password)Open a new pooled connection using the current URL and the specified user name and password.javax.naming.ReferencegetReference()Get a new reference for this object, using the current settings.java.lang.StringgetUrl()Get the current URL.java.lang.StringgetURL()Get the current URL.java.lang.StringgetUser()Get the current user name.javax.sql.XAConnectiongetXAConnection()Open a new XA connection using the current URL, user name and password.javax.sql.XAConnectiongetXAConnection(java.lang.String user, java.lang.String password)Open a new XA connection using the current URL and the specified user name and password.private voidinitFactory()booleanisWrapperFor(java.lang.Class<?> iface)Checks if unwrap can return an object of this class.private voidreadObject(java.io.ObjectInputStream in)Called when de-serializing the object.voidsetDescription(java.lang.String description)Set the description.voidsetLoginTimeout(int timeout)Set the login timeout in seconds, 0 meaning no timeout.voidsetLogWriter(java.io.PrintWriter out)Set the current log writer for this object.voidsetPassword(java.lang.String password)Set the current password.voidsetPasswordChars(char[] password)Set the current password in the form of a char array.voidsetUrl(java.lang.String url)Set the current URL.voidsetURL(java.lang.String url)Set the current URL.voidsetUser(java.lang.String user)Set the current user name.java.lang.StringtoString()INTERNAL<T> Tunwrap(java.lang.Class<T> iface)Return an object of this class if possible.-
Methods inherited from class org.h2.message.TraceObject
debugCode, debugCodeAssign, debugCodeCall, debugCodeCall, debugCodeCall, getNextId, getTraceId, getTraceObjectName, isDebugEnabled, isInfoEnabled, logAndConvert, quote, quoteArray, quoteBigDecimal, quoteBytes, quoteDate, quoteIntArray, quoteMap, quoteTime, quoteTimestamp, setTrace, unsupported
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
factory
private transient JdbcDataSourceFactory factory
-
logWriter
private transient java.io.PrintWriter logWriter
-
loginTimeout
private int loginTimeout
-
userName
private java.lang.String userName
-
passwordChars
private char[] passwordChars
-
url
private java.lang.String url
-
description
private java.lang.String description
-
-
Method Detail
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundExceptionCalled when de-serializing the object.- Parameters:
in- the input stream- Throws:
java.io.IOException- on failurejava.lang.ClassNotFoundException- on failure
-
initFactory
private void initFactory()
-
getLoginTimeout
public int getLoginTimeout()
Get the login timeout in seconds, 0 meaning no timeout.- Specified by:
getLoginTimeoutin interfacejavax.sql.CommonDataSource- Specified by:
getLoginTimeoutin interfacejavax.sql.ConnectionPoolDataSource- Specified by:
getLoginTimeoutin interfacejavax.sql.DataSource- Specified by:
getLoginTimeoutin interfacejavax.sql.XADataSource- Returns:
- the timeout in seconds
-
setLoginTimeout
public void setLoginTimeout(int timeout)
Set the login timeout in seconds, 0 meaning no timeout. The default value is 0. This value is ignored by this database.- Specified by:
setLoginTimeoutin interfacejavax.sql.CommonDataSource- Specified by:
setLoginTimeoutin interfacejavax.sql.ConnectionPoolDataSource- Specified by:
setLoginTimeoutin interfacejavax.sql.DataSource- Specified by:
setLoginTimeoutin interfacejavax.sql.XADataSource- Parameters:
timeout- the timeout in seconds
-
getLogWriter
public java.io.PrintWriter getLogWriter()
Get the current log writer for this object.- Specified by:
getLogWriterin interfacejavax.sql.CommonDataSource- Specified by:
getLogWriterin interfacejavax.sql.ConnectionPoolDataSource- Specified by:
getLogWriterin interfacejavax.sql.DataSource- Specified by:
getLogWriterin interfacejavax.sql.XADataSource- Returns:
- the log writer
-
setLogWriter
public void setLogWriter(java.io.PrintWriter out)
Set the current log writer for this object. This value is ignored by this database.- Specified by:
setLogWriterin interfacejavax.sql.CommonDataSource- Specified by:
setLogWriterin interfacejavax.sql.ConnectionPoolDataSource- Specified by:
setLogWriterin interfacejavax.sql.DataSource- Specified by:
setLogWriterin interfacejavax.sql.XADataSource- Parameters:
out- the log writer
-
getConnection
public java.sql.Connection getConnection() throws java.sql.SQLExceptionOpen a new connection using the current URL, user name and password.- Specified by:
getConnectionin interfacejavax.sql.DataSource- Returns:
- the connection
- Throws:
java.sql.SQLException
-
getConnection
public java.sql.Connection getConnection(java.lang.String user, java.lang.String password) throws java.sql.SQLExceptionOpen a new connection using the current URL and the specified user name and password.- Specified by:
getConnectionin interfacejavax.sql.DataSource- Parameters:
user- the user namepassword- the password- Returns:
- the connection
- Throws:
java.sql.SQLException
-
getURL
public java.lang.String getURL()
Get the current URL.- Returns:
- the URL
-
setURL
public void setURL(java.lang.String url)
Set the current URL.- Parameters:
url- the new URL
-
getUrl
public java.lang.String getUrl()
Get the current URL. This method does the same as getURL, but this methods signature conforms the JavaBean naming convention.- Returns:
- the URL
-
setUrl
public void setUrl(java.lang.String url)
Set the current URL. This method does the same as setURL, but this methods signature conforms the JavaBean naming convention.- Parameters:
url- the new URL
-
setPassword
public void setPassword(java.lang.String password)
Set the current password.- Parameters:
password- the new password.
-
setPasswordChars
public void setPasswordChars(char[] password)
Set the current password in the form of a char array.- Parameters:
password- the new password in the form of a char array.
-
convertToString
private static java.lang.String convertToString(char[] a)
-
getPassword
public java.lang.String getPassword()
Get the current password.- Returns:
- the password
-
getUser
public java.lang.String getUser()
Get the current user name.- Returns:
- the user name
-
setUser
public void setUser(java.lang.String user)
Set the current user name.- Parameters:
user- the new user name
-
getDescription
public java.lang.String getDescription()
Get the current description.- Returns:
- the description
-
setDescription
public void setDescription(java.lang.String description)
Set the description.- Parameters:
description- the new description
-
getReference
public javax.naming.Reference getReference()
Get a new reference for this object, using the current settings.- Specified by:
getReferencein interfacejavax.naming.Referenceable- Returns:
- the new reference
-
getXAConnection
public javax.sql.XAConnection getXAConnection() throws java.sql.SQLExceptionOpen a new XA connection using the current URL, user name and password.- Specified by:
getXAConnectionin interfacejavax.sql.XADataSource- Returns:
- the connection
- Throws:
java.sql.SQLException
-
getXAConnection
public javax.sql.XAConnection getXAConnection(java.lang.String user, java.lang.String password) throws java.sql.SQLExceptionOpen a new XA connection using the current URL and the specified user name and password.- Specified by:
getXAConnectionin interfacejavax.sql.XADataSource- Parameters:
user- the user namepassword- the password- Returns:
- the connection
- Throws:
java.sql.SQLException
-
getPooledConnection
public javax.sql.PooledConnection getPooledConnection() throws java.sql.SQLExceptionOpen a new pooled connection using the current URL, user name and password.- Specified by:
getPooledConnectionin interfacejavax.sql.ConnectionPoolDataSource- Returns:
- the connection
- Throws:
java.sql.SQLException
-
getPooledConnection
public javax.sql.PooledConnection getPooledConnection(java.lang.String user, java.lang.String password) throws java.sql.SQLExceptionOpen a new pooled connection using the current URL and the specified user name and password.- Specified by:
getPooledConnectionin interfacejavax.sql.ConnectionPoolDataSource- Parameters:
user- the user namepassword- the password- Returns:
- the connection
- Throws:
java.sql.SQLException
-
unwrap
public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLExceptionReturn an object of this class if possible.- Specified by:
unwrapin interfacejava.sql.Wrapper- Parameters:
iface- the class- Returns:
- this
- Throws:
java.sql.SQLException
-
isWrapperFor
public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLExceptionChecks if unwrap can return an object of this class.- Specified by:
isWrapperForin interfacejava.sql.Wrapper- Parameters:
iface- the class- Returns:
- whether or not the interface is assignable from this class
- Throws:
java.sql.SQLException
-
getParentLogger
public java.util.logging.Logger getParentLogger()
[Not supported]- Specified by:
getParentLoggerin interfacejavax.sql.CommonDataSource
-
toString
public java.lang.String toString()
INTERNAL- Overrides:
toStringin classjava.lang.Object
-
-