objects. The factory uses the supplied
configuration to obtain a 3rd-party datasource or to create one, and
to setup prepared statement caching.
| Method from org.apache.openjpa.jdbc.schema.DataSourceFactory Detail: |
public static void closeDataSource(DataSource ds) {
if (ds instanceof DelegatingDataSource)
ds = ((DelegatingDataSource) ds).getInnermostDelegate();
ImplHelper.close(ds);
}
Close the given data source. |
public static DecoratingDataSource decorateDataSource(DataSource ds,
JDBCConfiguration conf,
boolean factory2) {
Options opts = Configurations.parseProperties((factory2)
? conf.getConnectionFactory2Properties()
: conf.getConnectionFactoryProperties());
Log jdbcLog = conf.getLog(JDBCConfiguration.LOG_JDBC);
Log sqlLog = conf.getLog(JDBCConfiguration.LOG_SQL);
DecoratingDataSource dds = new DecoratingDataSource(ds);
try {
// add user-defined decorators
List decorators = new ArrayList();
decorators.addAll(Arrays.asList(conf.
getConnectionDecoratorInstances()));
// add jdbc events decorator
JDBCEventConnectionDecorator ecd =
new JDBCEventConnectionDecorator();
Configurations.configureInstance(ecd, conf, opts);
JDBCListener[] listeners = conf.getJDBCListenerInstances();
for (int i = 0; i < listeners.length; i++)
ecd.addListener(listeners[i]);
decorators.add(ecd);
// ask the DriverDataSource to provide any additional decorators
if (ds instanceof DriverDataSource) {
List decs = ((DriverDataSource) ds).
createConnectionDecorators();
if (decs != null)
decorators.addAll(decs);
}
// logging decorator
LoggingConnectionDecorator lcd =
new LoggingConnectionDecorator();
Configurations.configureInstance(lcd, conf, opts);
lcd.getLogs().setJDBCLog(jdbcLog);
lcd.getLogs().setSQLLog(sqlLog);
decorators.add(lcd);
dds.addDecorators(decorators);
return dds;
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new StoreException(e).setFatal(true);
}
}
Install listeners and base decorators. |
public static DataSource defaultsDataSource(DataSource ds,
String user,
String pass) {
if (user == null && pass == null)
return ds;
// also check if they are both blank strings
if ("".equals(user) && "".equals(pass))
return ds;
return new DefaultsDataSource(ds, user, pass);
}
Return a data source with the given user name and password
pre-configured as the defaults when DataSource#getConnection
is called. |
public static DecoratingDataSource installDBDictionary(DBDictionary dict,
DecoratingDataSource ds,
JDBCConfiguration conf,
boolean factory2) {
DataSource inner = ds.getInnermostDelegate();
if (inner instanceof DriverDataSource)
((DriverDataSource) inner).initDBDictionary(dict);
Connection conn = null;
try {
// add the dictionary as a warning handler on the logging
// decorator
ConnectionDecorator cd;
for (Iterator itr = ds.getDecorators().iterator(); itr.hasNext();) {
cd = (ConnectionDecorator) itr.next();
if (cd instanceof LoggingConnectionDecorator)
((LoggingConnectionDecorator) cd).setWarningHandler(dict);
}
// misc configuration connection decorator (statement timeouts,
// transaction isolation, etc)
ConfiguringConnectionDecorator ccd =
new ConfiguringConnectionDecorator();
ccd.setTransactionIsolation(conf.getTransactionIsolationConstant());
if (factory2 || !conf.isConnectionFactoryModeManaged()) {
if (!dict.supportsMultipleNontransactionalResultSets)
ccd.setAutoCommit(Boolean.FALSE);
else
ccd.setAutoCommit(Boolean.TRUE);
}
Options opts = Configurations.parseProperties((factory2)
? conf.getConnectionFactory2Properties()
: conf.getConnectionFactoryProperties());
Configurations.configureInstance(ccd, conf, opts);
ds.addDecorator(ccd);
// allow the dbdictionary to decorate the connection further
ds.addDecorator(dict);
// ensure dbdictionary to process connectedConfiguration()
if (!factory2)
conn = ds.getConnection(conf.getConnectionUserName(), conf
.getConnectionPassword());
else
conn = ds.getConnection(conf.getConnection2UserName(), conf
.getConnection2Password());
return ds;
} catch (Exception e) {
throw new StoreException(e).setFatal(true);
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException se) {
// ignore any exception since the connection is not going
// to be used anyway
}
}
}
Install things deferred until the DBDictionary instance is available. |
public static DataSource newDataSource(JDBCConfiguration conf,
boolean factory2) {
String driver = (factory2) ? conf.getConnection2DriverName()
: conf.getConnectionDriverName();
if (StringUtils.isEmpty(driver))
throw new UserException(_loc.get("no-driver", driver)).
setFatal(true);
ClassLoader loader = conf.getClassResolverInstance().
getClassLoader(DataSourceFactory.class, null);
String props = (factory2) ? conf.getConnection2Properties()
: conf.getConnectionProperties();
try {
Class driverClass;
try {
driverClass = Class.forName(driver, true, loader);
} catch (ClassNotFoundException cnfe) {
// try with the core class loader
driverClass = Class.forName(driver);
}
if (Driver.class.isAssignableFrom(driverClass)) {
DriverDataSource ds = conf.newDriverDataSourceInstance();
ds.setClassLoader(loader);
ds.setConnectionDriverName(driver);
ds.setConnectionProperties(Configurations.
parseProperties(props));
if (!factory2) {
ds.setConnectionFactoryProperties(Configurations.
parseProperties(conf.getConnectionFactoryProperties()));
ds.setConnectionURL(conf.getConnectionURL());
ds.setConnectionUserName(conf.getConnectionUserName());
ds.setConnectionPassword(conf.getConnectionPassword());
} else {
ds.setConnectionFactoryProperties
(Configurations.parseProperties(conf.
getConnectionFactory2Properties()));
ds.setConnectionURL(conf.getConnection2URL());
ds.setConnectionUserName(conf.getConnection2UserName());
ds.setConnectionPassword(conf.getConnection2Password());
}
return ds;
}
// see if their driver name is actually a data source
if (DataSource.class.isAssignableFrom(driverClass)) {
return (DataSource) Configurations.newInstance(driver,
conf, props, (ClassLoader) AccessController.doPrivileged(
J2DoPrivHelper.getClassLoaderAction(
DataSource.class)));
}
}
catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new StoreException(e).setFatal(true);
}
// not a driver or a data source; die
throw new UserException(_loc.get("bad-driver", driver)).setFatal(true);
}
Create a datasource using the given configuration. |