Top Description Fields Constructors Methods
org.apache.derby.jdbc

public Class EmbeddedDataSource

extends ReferenceableDataSource
implements Referenceable
Class Inheritance
All Implemented Interfaces
javax.naming.Referenceable
Known Direct Subclasses
org.apache.derby.jdbc.EmbeddedDataSource40, org.apache.derby.jdbc.EmbeddedXADataSource, org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource
Imports
java.lang.reflect.InvocationTargetException, .Method, .Modifier, javax.naming.NamingException, .Reference, .Referenceable, .StringRefAddr

This data source is suitable for an application using embedded Derby, running on full Java SE 6 and higher, corresponding to 4.0 and higher.

A DataSource is a factory for Connection objects. An object that implements the DataSource interface will typically be registered with a JNDI service provider.

EmbeddedDataSource automatically supports the correct JDBC specification version for the Java Virtual Machine's environment.

Use BasicEmbeddedDataSource40 if your application runs on Java 8 Compact Profile 2.

The following is a list of properties that can be set on a Derby DataSource object:

Standard DataSource properties (from JDBC 3.0 specification).


Derby specific DataSource properties.

Examples.

This is an example of setting a property directly using Derby's EmbeddedDataSource object. This code is typically written by a system integrator :

 

 import org.apache.derby.jdbc.*;

 // dbname is the database name
 // if create is true, create the database if necessary
 javax.sql.DataSource makeDataSource (String dbname, boolean create)
	throws Throwable 
 { 
	EmbeddedDataSource ds = new EmbeddedDataSource(); 
	ds.setDatabaseName(dbname);

	if (create)
		ds.setCreateDatabase("create");

	return ds;
 }
	

Example of setting properties thru reflection. This code is typically generated by tools or written by a system integrator:


 javax.sql.DataSource makeDataSource(String dbname) 
	throws Throwable 
 {
	Class[] parameter = new Class[1];
	parameter[0] = dbname.getClass();
	DataSource ds =  new EmbeddedDataSource();
	Class cl = ds.getClass();

	Method setName = cl.getMethod("setDatabaseName", parameter);
	Object[] arg = new Object[1];
	arg[0] = dbname;
	setName.invoke(ds, arg);

	return ds;
 }
	

Example on how to register a data source object with a JNDI naming service.

 DataSource ds = makeDataSource("mydb");
 Context ctx = new InitialContext();
 ctx.bind("jdbc/MyDB", ds);
	

Example on how to retrieve a data source object from a JNDI naming service.

 Context ctx = new InitialContext();
 DataSource ds = (DataSource)ctx.lookup("jdbc/MyDB");
	

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
public
EmbeddedDataSource()

No-arg constructor.

Method Summary

Modifier and TypeMethod and Description
private static void
addBeanProperties(Object
this object
ths
,
Reference
The reference to add properties to
ref
)

Add Java Bean properties to the reference using StringRefAddr for each property.

public Object
getObjectInstance(Object
the possibly null object containing location or reference information that can be used in creating an object
refObj
,
Name
the name of this object relative to nameContext, or null if no name is specified
name
,
Context
context relative to which the name parameter is specified, or null if name is relative to the default initial context.
nameContext
,
Hashtable<?, ?>
possibly null environment that is used in creating the object.
environment
)

Overrides org.apache.derby.jdbc.ReferenceableDataSource.getObjectInstance.

Implements javax.naming.spi.ObjectFactory.getObjectInstance.

Reconstructs a Derby embedded-driver data source object from a JNDI data source reference.
public final Reference

Returns:

the created reference object for this data source
getReference
()

Implements javax.naming.Referenceable.getReference.

This method creates a new Reference object to represent this data source.

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.derby.jdbc.ReferenceableDataSource.serialVersionUID.

Constructor Detail

EmbeddedDataSourceback to summary
public EmbeddedDataSource()

No-arg constructor.

Method Detail

addBeanPropertiesback to summary
private static void addBeanProperties(Object ths, Reference ref)

Add Java Bean properties to the reference using StringRefAddr for each property. List of bean properties is defined from the public getXXX() methods on this object that take no arguments and return short, int, boolean or String. The StringRefAddr has a key of the Java bean property name, converted from the method name. E.g. traceDirectory for traceDirectory.

Parameters
ths:Object

this object

ref:Reference

The reference to add properties to

getObjectInstanceback to summary
public Object getObjectInstance(Object refObj, Name name, Context nameContext, Hashtable<?, ?> environment) throws Exception

Overrides org.apache.derby.jdbc.ReferenceableDataSource.getObjectInstance.

Implements javax.naming.spi.ObjectFactory.getObjectInstance.

Doc from org.apache.derby.jdbc.ReferenceableDataSource.getObjectInstance.

Reconstructs a Derby embedded-driver data source object from a JNDI data source reference.

The getObjectInstance method is passed a reference that corresponds to the object being retrieved as its first parameter. The other parameters are optional in the case of JDBC data source objects. The object factory should use the information contained in the reference to reconstruct the data source. If for some reason, a data source object cannot be reconstructed from the reference, a value of null may be returned. This allows other object factories that may be registered in JNDI to be tried. If an exception is thrown then no other object factories are tried.

Parameters
refObj:Object

the possibly null object containing location or reference information that can be used in creating an object

name:Name

the name of this object relative to nameContext, or null if no name is specified

nameContext:Context

context relative to which the name parameter is specified, or null if name is relative to the default initial context.

environment:Hashtable<?, ?>

possibly null environment that is used in creating the object.

Returns:Object

Object created, or null if no attempt to create the object is made.

Annotations
@Override
Exceptions
Exception:
if recreating the object fails
getReferenceback to summary
public final Reference getReference() throws NamingException

Implements javax.naming.Referenceable.getReference.

This method creates a new Reference object to represent this data source. The class name of the data source object is saved in the Reference, so that an object factory will know that it should create an instance of that class when a lookup operation is performed. The class is also stored in the reference. This is not required by JNDI, but is recommend in practice. JNDI will always use the object factory class specified in the reference when reconstructing an object, if a class name has been specified. See the JNDI SPI documentation for further details on this topic, and for a complete description of the Reference and StringRefAddr classes.

Derby data source classes class provides several standard JDBC properties. The names and values of the data source properties are also stored in the reference using the StringRefAddr class. This is all the information needed to reconstruct an embedded data source object.

Returns:Reference

the created reference object for this data source

Annotations
@Override
Exceptions
NamingException:
cannot find named object