org.datanucleus.store.rdbms.ConnectionFactoryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-rdbms Show documentation
Show all versions of datanucleus-rdbms Show documentation
Plugin for DataNucleus providing persistence to RDBMS datastores.
/**********************************************************************
Copyright (c) 2007 Erik Bengtson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus.store.rdbms;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ExecutionContext;
import org.datanucleus.PropertyNames;
import org.datanucleus.Transaction;
import org.datanucleus.exceptions.ClassNotResolvedException;
import org.datanucleus.exceptions.ConnectionFactoryNotFoundException;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.exceptions.UnsupportedConnectionFactoryException;
import org.datanucleus.store.StoreManager;
import org.datanucleus.store.connection.AbstractConnectionFactory;
import org.datanucleus.store.connection.AbstractManagedConnection;
import org.datanucleus.store.connection.ConnectionFactory;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.rdbms.adapter.DatastoreAdapter;
import org.datanucleus.store.rdbms.datasource.DataNucleusDataSourceFactory;
import org.datanucleus.transaction.TransactionIsolation;
import org.datanucleus.transaction.TransactionUtils;
import org.datanucleus.util.JavaUtils;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;
/**
* ConnectionFactory for RDBMS datastores.
* Each instance is a factory of Transactional connection or NonTransactional connection.
*/
public class ConnectionFactoryImpl extends AbstractConnectionFactory
{
protected static final Localiser LOCALISER_RDBMS = Localiser.getInstance(
"org.datanucleus.store.rdbms.Localisation", RDBMSStoreManager.class.getClassLoader());
/** Datasources. */
DataSource[] dataSources;
/**
* Constructor.
* @param storeMgr Store Manager
* @param resourceType either tx or nontx
*/
public ConnectionFactoryImpl(StoreManager storeMgr, String resourceType)
{
super(storeMgr, resourceType);
if (resourceType.equals("tx"))
{
// JTA needs the primary DataSource to be present always
initialiseDataSources();
}
}
/**
* Method to initialise the DataSource(s) used by this ConnectionFactory.
* Only invoked when the request for the first connection comes in.
*/
protected synchronized void initialiseDataSources()
{
if (resourceType.equals("tx"))
{
// Transactional
String configuredResourceTypeProperty = storeMgr.getStringProperty(DATANUCLEUS_CONNECTION_RESOURCE_TYPE);
if (configuredResourceTypeProperty != null)
{
if (options == null)
{
options = new HashMap();
}
options.put(ConnectionFactory.RESOURCE_TYPE_OPTION, configuredResourceTypeProperty);
}
String requiredPoolingType = storeMgr.getStringProperty(PropertyNames.PROPERTY_CONNECTION_POOLINGTYPE);
Object connDS = storeMgr.getConnectionFactory();
String connJNDI = storeMgr.getConnectionFactoryName();
String connURL = storeMgr.getConnectionURL();
dataSources = generateDataSources(storeMgr, connDS, connJNDI, resourceType, requiredPoolingType, connURL);
if (dataSources == null)
{
throw new NucleusUserException(LOCALISER_RDBMS.msg("047009", "transactional")).setFatal();
}
}
else
{
// Non-transactional
String configuredResourceTypeProperty = storeMgr.getStringProperty(DATANUCLEUS_CONNECTION2_RESOURCE_TYPE);
if (configuredResourceTypeProperty!=null)
{
if (options == null)
{
options = new HashMap();
}
options.put(ConnectionFactory.RESOURCE_TYPE_OPTION, configuredResourceTypeProperty);
}
String requiredPoolingType = storeMgr.getStringProperty(PropertyNames.PROPERTY_CONNECTION_POOLINGTYPE2);
if (requiredPoolingType == null)
{
requiredPoolingType = storeMgr.getStringProperty(PropertyNames.PROPERTY_CONNECTION_POOLINGTYPE);
}
Object connDS = storeMgr.getConnectionFactory2();
String connJNDI = storeMgr.getConnectionFactory2Name();
String connURL = storeMgr.getConnectionURL();
dataSources = generateDataSources(storeMgr, connDS, connJNDI, resourceType, requiredPoolingType, connURL);
if (dataSources == null)
{
// Fallback to transactional settings
connDS = storeMgr.getConnectionFactory();
connJNDI = storeMgr.getConnectionFactoryName();
dataSources = generateDataSources(storeMgr, connDS, connJNDI, resourceType, requiredPoolingType, connURL);
}
if (dataSources == null)
{
throw new NucleusUserException(LOCALISER_RDBMS.msg("047009", "non-transactional")).setFatal();
}
}
}
/**
* Method to generate the datasource(s) used by this connection factory.
* Searches initially for a provided DataSource, then if not found, for JNDI DataSource(s), and finally
* for the DataSource at a connection URL.
* @param storeMgr Store Manager
* @param connDS Factory data source object
* @param connJNDI DataSource JNDI name(s)
* @param connURL URL for connections
* @return The DataSource(s)
*/
private static DataSource[] generateDataSources(StoreManager storeMgr, Object connDS, String connJNDI,
String resourceType, String requiredPoolingType, String connURL)
{
DataSource[] dataSources = null;
if (connDS != null)
{
if (!(connDS instanceof DataSource) && !(connDS instanceof XADataSource))
{
throw new UnsupportedConnectionFactoryException(connDS);
}
dataSources = new DataSource[1];
dataSources[0] = (DataSource) connDS;
}
else if (connJNDI != null)
{
String[] connectionFactoryNames = StringUtils.split(connJNDI, ",");
dataSources = new DataSource[connectionFactoryNames.length];
for (int i=0; i= 0)
{
// Override with the adapters required isolation level
reqdIsolationLevel = rdba.getRequiredTransactionIsolationLevel();
}
cnx = connProvider.getConnection(dataSources);
boolean succeeded = false;
try
{
if (cnx.isReadOnly() != readOnly)
{
NucleusLogger.CONNECTION.debug("Setting readonly=" + readOnly + " to connection: " +
cnx.toString());
cnx.setReadOnly(readOnly);
}
if (reqdIsolationLevel == TransactionIsolation.TRANSACTION_NONE)
{
if (!cnx.getAutoCommit())
{
cnx.setAutoCommit(true);
}
}
else
{
if (cnx.getAutoCommit())
{
cnx.setAutoCommit(false);
}
if (rdba.supportsTransactionIsolation(reqdIsolationLevel))
{
int currentIsolationLevel = cnx.getTransactionIsolation();
if (currentIsolationLevel != reqdIsolationLevel)
{
cnx.setTransactionIsolation(reqdIsolationLevel);
}
}
else
{
NucleusLogger.CONNECTION.warn(LOCALISER_RDBMS.msg("051008", reqdIsolationLevel));
}
}
if (NucleusLogger.CONNECTION.isDebugEnabled())
{
NucleusLogger.CONNECTION.debug(LOCALISER_RDBMS.msg("052002", StringUtils.toJVMIDString(cnx),
TransactionUtils.getNameForTransactionIsolationLevel(reqdIsolationLevel),
cnx.getAutoCommit()));
}
if (reqdIsolationLevel != isolation && isolation == TransactionIsolation.TRANSACTION_NONE)
{
// User asked for a level that implies auto-commit so make sure it has that
if (!cnx.getAutoCommit())
{
NucleusLogger.CONNECTION.debug("Setting autocommit=true for connection: "+StringUtils.toJVMIDString(cnx));
cnx.setAutoCommit(true);
}
}
succeeded = true;
}
catch (SQLException e)
{
throw new NucleusDataStoreException(e.getMessage(),e);
}
finally
{
if (!succeeded)
{
try
{
cnx.close();
}
catch (SQLException e)
{
}
if (NucleusLogger.CONNECTION.isDebugEnabled())
{
NucleusLogger.CONNECTION.debug(LOCALISER_RDBMS.msg("052003", StringUtils.toJVMIDString(cnx)));
}
}
}
}
else
{
// Create basic Connection since no DatastoreAdapter created yet
cnx = dataSources[0].getConnection();
if (cnx == null)
{
String msg = LOCALISER_RDBMS.msg("052000", dataSources[0]);
NucleusLogger.CONNECTION.error(msg);
throw new NucleusDataStoreException(msg);
}
if (NucleusLogger.CONNECTION.isDebugEnabled())
{
NucleusLogger.CONNECTION.debug(LOCALISER_RDBMS.msg("052001", StringUtils.toJVMIDString(cnx)));
}
}
}
catch (SQLException e)
{
throw new NucleusDataStoreException(e.getMessage(),e);
}
this.conn = cnx;
}
needsCommitting = true;
return this.conn;
}
/**
* Close the connection
*/
public void close()
{
for (int i=0; i