Package org.h2.jdbcx

# Class JdbcDataSource

java.lang.Object
org.h2.message.TraceObject
org.h2.jdbcx.JdbcDataSource

All Implemented Interfaces:
  `Serializable`, `Wrapper`, `Referenceable`, `CommonDataSource`, `ConnectionPoolDataSource`, `DataSource`, `XADataSource`, `JdbcDataSourceBackwardsCompat`

public final class JdbcDataSource extends TraceObject implements XADataSource, DataSource, ConnectionPoolDataSource, Serializable, 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 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

Modifier and Type
Method
Description
`Connection`
`getConnection()`
Open a new connection using the current URL, user name and password.
`Connection`
`getConnection(String user, String password)`
Open a new connection using the current URL and the specified user name and password.
`String`
`getDescription()`
Get the current description.
`int`
`getLoginTimeout()`
Get the login timeout in seconds, 0 meaning no timeout.
`PrintWriter`
`getLogWriter()`
Get the current log writer for this object.
`Logger`
`getParentLogger()`
[Not supported]
`String`
`getPassword()`
Get the current password.
`PooledConnection`
`getPooledConnection()`
Open a new pooled connection using the current URL, user name and password.
`PooledConnection`
`getPooledConnection(String user, String password)`
Open a new pooled connection using the current URL and the specified user name and password.
`Reference`
`getReference()`
Get a new reference for this object, using the current settings.
`String`
`getUrl()`
Get the current URL.
`String`
`getURL()`
Get the current URL.
`String`
`getUser()`
Get the current user name.
`XAConnection`
`getXAConnection()`
Open a new XA connection using the current URL, user name and password.
`XAConnection`
`getXAConnection(String user, String password)`
Open a new XA connection using the current URL and the specified user name and password.
`boolean`
`isWrapperFor(Class<?> iface)`
Checks if unwrap can return an object of this class.
`void`
`setDescription(String description)`
Set the description.
`void`
`setLoginTimeout(int timeout)`
Set the login timeout in seconds, 0 meaning no timeout.
`void`
`setLogWriter(PrintWriter out)`
Set the current log writer for this object.
`void`
`setPassword(String password)`
Set the current password.
`void`
`setPasswordChars(char[] password)`
Set the current password in the form of a char array.
`void`
`setUrl(String url)`
Set the current URL.
`void`
`setURL(String url)`
Set the current URL.
`void`
`setUser(String user)`
Set the current user name.
`String`
`toString()`
INTERNAL
`<T> T`
`unwrap(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`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface javax.sql.CommonDataSource

`createShardingKeyBuilder`

### Methods inherited from interface javax.sql.ConnectionPoolDataSource

`createPooledConnectionBuilder`

### Methods inherited from interface javax.sql.DataSource

`createConnectionBuilder`

### Methods inherited from interface javax.sql.XADataSource

`createXAConnectionBuilder`

## Constructor Details

### JdbcDataSource

public JdbcDataSource()
The public constructor.

## Method Details

### getLoginTimeout

public int getLoginTimeout()
Get the login timeout in seconds, 0 meaning no timeout.

Specified by:
  `getLoginTimeout` in interface `CommonDataSource`
Specified by:
  `getLoginTimeout` in interface `ConnectionPoolDataSource`
Specified by:
  `getLoginTimeout` in interface `DataSource`
Specified by:
  `getLoginTimeout` in interface `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:
  `setLoginTimeout` in interface `CommonDataSource`
Specified by:
  `setLoginTimeout` in interface `ConnectionPoolDataSource`
Specified by:
  `setLoginTimeout` in interface `DataSource`
Specified by:
  `setLoginTimeout` in interface `XADataSource`
Parameters:
  `timeout` - the timeout in seconds

### getLogWriter

public PrintWriter getLogWriter()
Get the current log writer for this object.

Specified by:
  `getLogWriter` in interface `CommonDataSource`
Specified by:
  `getLogWriter` in interface `ConnectionPoolDataSource`
Specified by:
  `getLogWriter` in interface `DataSource`
Specified by:
  `getLogWriter` in interface `XADataSource`
Returns:
  the log writer

### setLogWriter

public void setLogWriter(PrintWriter out)
Set the current log writer for this object. This value is ignored by this database.

Specified by:
  `setLogWriter` in interface `CommonDataSource`
Specified by:
  `setLogWriter` in interface `ConnectionPoolDataSource`
Specified by:
  `setLogWriter` in interface `DataSource`
Specified by:
  `setLogWriter` in interface `XADataSource`
Parameters:
  `out` - the log writer

### getConnection

public Connection getConnection() throws SQLException
Open a new connection using the current URL, user name and password.

Specified by:
  `getConnection` in interface `DataSource`
Returns:
  the connection
Throws:
  `SQLException`

### getConnection

public Connection getConnection(String user, String password) throws SQLException
Open a new connection using the current URL and the specified user name and password.

Specified by:
  `getConnection` in interface `DataSource`
Parameters:
  `user` - the user name
  `password` - the password
Returns:
  the connection
Throws:
  `SQLException`

### getURL

public String getURL()
Get the current URL.

Returns:
  the URL

### setURL

public void setURL(String url)
Set the current URL.

Parameters:
  `url` - the new URL

### getUrl

public 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(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(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.

### getPassword

public String getPassword()
Get the current password.

Returns:
  the password

### getUser

public String getUser()
Get the current user name.

Returns:
  the user name

### setUser

public void setUser(String user)
Set the current user name.

Parameters:
  `user` - the new user name

### getDescription

public String getDescription()
Get the current description.

Returns:
  the description

### setDescription

public void setDescription(String description)
Set the description.

Parameters:
  `description` - the new description

### getReference

public Reference getReference()
Get a new reference for this object, using the current settings.

Specified by:
  `getReference` in interface `Referenceable`
Returns:
  the new reference

### getXAConnection

public XAConnection getXAConnection() throws SQLException
Open a new XA connection using the current URL, user name and password.

Specified by:
  `getXAConnection` in interface `XADataSource`
Returns:
  the connection
Throws:
  `SQLException`

### getXAConnection

public XAConnection getXAConnection(String user, String password) throws SQLException
Open a new XA connection using the current URL and the specified user name and password.

Specified by:
  `getXAConnection` in interface `XADataSource`
Parameters:
  `user` - the user name
  `password` - the password
Returns:
  the connection
Throws:
  `SQLException`

### getPooledConnection

public PooledConnection getPooledConnection() throws SQLException
Open a new pooled connection using the current URL, user name and password.

Specified by:
  `getPooledConnection` in interface `ConnectionPoolDataSource`
Returns:
  the connection
Throws:
  `SQLException`

### getPooledConnection

public PooledConnection getPooledConnection(String user, String password) throws SQLException
Open a new pooled connection using the current URL and the specified user name and password.

Specified by:
  `getPooledConnection` in interface `ConnectionPoolDataSource`
Parameters:
  `user` - the user name
  `password` - the password
Returns:
  the connection
Throws:
  `SQLException`

### unwrap

public <T> T unwrap(Class<T> iface) throws SQLException
Return an object of this class if possible.

Specified by:
  `unwrap` in interface `Wrapper`
Parameters:
  `iface` - the class
Returns:
  this
Throws:
  `SQLException`

### isWrapperFor

public boolean isWrapperFor(Class<?> iface) throws SQLException
Checks if unwrap can return an object of this class.

Specified by:
  `isWrapperFor` in interface `Wrapper`
Parameters:
  `iface` - the class
Returns:
  whether or not the interface is assignable from this class
Throws:
  `SQLException`

### getParentLogger

public Logger getParentLogger()
[Not supported]

Specified by:
  `getParentLogger` in interface `CommonDataSource`

### toString

public String toString()
INTERNAL

Overrides:
  `toString` in class `Object`