db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattba...@apache.org
Subject cvs commit: db-ojb/src/jca/org/apache/ojb/otm/connector OTMJCAManagedConnection.java OTMJCAConnection.java
Date Wed, 02 Jul 2003 17:33:49 GMT
mattbaird    2003/07/02 10:33:49

  Modified:    src/jca/org/apache/ojb/otm/connector
                        OTMJCAManagedConnection.java OTMJCAConnection.java
  Log:
  updates to connector
  
  Revision  Changes    Path
  1.2       +73 -45    db-ojb/src/jca/org/apache/ojb/otm/connector/OTMJCAManagedConnection.java
  
  Index: OTMJCAManagedConnection.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/jca/org/apache/ojb/otm/connector/OTMJCAManagedConnection.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- OTMJCAManagedConnection.java	1 Jul 2003 21:04:27 -0000	1.1
  +++ OTMJCAManagedConnection.java	2 Jul 2003 17:33:48 -0000	1.2
  @@ -80,11 +80,12 @@
   	private boolean m_destroyed;
   	private final Set m_handles = new HashSet();
   	private final Collection m_connectionEventListeners = new ArrayList();
  -
  +	private boolean m_managed = false;
   	/**
   	 * the wrapped connection
   	 */
   	private OTMConnection m_connection;
  +	private Transaction m_tx;
   
   	OTMJCAManagedConnection(ManagedConnectionFactory mcf, OTMConnection conn, PBKey pbKey)
   	{
  @@ -94,16 +95,19 @@
   		m_connection = conn;
   	}
   
  -	public OTMConnection getConnection()
  +	/**
  +	 * get the underlying wrapped connection
  +	 * @return OTMConnection raw connection to the OTM.
  +	 */
  +	OTMConnection getConnection()
   	{
  +		if (m_connection == null)
  +		{
  +			throw new OTMConnectionRuntimeException("Connection is null.");
  +		}
   		return m_connection;
   	}
   
  -	public void setConnection(OTMConnection connection)
  -	{
  -		m_connection = connection;
  -	}
  -
   	public String getUserName()
   	{
   		return m_pbKey.getUser();
  @@ -114,7 +118,10 @@
   	{
   		Util.log("In OTMJCAManagedConnection.getConnection");
   		OTMJCAConnection myCon = new OTMJCAConnection(this);
  -		m_handles.add(myCon);
  +		synchronized (m_handles)
  +		{
  +			m_handles.add(myCon);
  +		}
   		return myCon;
   	}
   
  @@ -122,6 +129,7 @@
   	{
   		Util.log("In OTMJCAManagedConnection.destroy");
   		cleanup();
  +		m_connection.close();
   		m_destroyed = true;
   	}
   
  @@ -133,7 +141,6 @@
   			for (Iterator i = m_handles.iterator(); i.hasNext();)
   			{
   				OTMJCAConnection lc = (OTMJCAConnection) i.next();
  -				lc.close();
   				lc.setManagedConnection(null);
   			}
   			m_handles.clear();
  @@ -163,9 +170,13 @@
   	public void associateConnection(Object connection)
   	{
   		Util.log("In OTMJCAManagedConnection.associateConnection");
  +		if (connection == null)
  +		{
  +			throw new OTMConnectionRuntimeException("Cannot associate a null connection");
  +		}
   		if (!(connection instanceof OTMJCAConnection))
   		{
  -			throw new OTMConnectionRuntimeException("Wrong kind of connection handle to associate"
+ connection);
  +			throw new OTMConnectionRuntimeException("Cannot associate a connection of type: " +
connection.getClass().getName() + " to a handle that manages: " + OTMJCAConnection.class.getName());
   		}
   		((OTMJCAConnection) connection).setManagedConnection(this);
   		synchronized (m_handles)
  @@ -194,9 +205,13 @@
   			throws ResourceException
   	{
   		Util.log("In OTMJCAManagedConnection.getXAResource");
  -		throw new NotSupportedException("public XAResource getXAResource()");
  +		throw new NotSupportedException("public XAResource getXAResource() not supported in this
release.");
   	}
   
  +	/**
  +	 * the OTMConnection is the transaction
  +	 * @return
  +	 */
   	public LocalTransaction getLocalTransaction()
   	{
   		Util.log("In OTMJCAManagedConnection.getLocalTransaction");
  @@ -230,21 +245,6 @@
   		return m_destroyed;
   	}
   
  -	protected void broadcastConnectionError(Exception e)
  -	{
  -		ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED,
e);
  -		Collection copy = null;
  -		synchronized (m_connectionEventListeners)
  -		{
  -			copy = new ArrayList(m_connectionEventListeners);
  -		}
  -		for (Iterator i = copy.iterator(); i.hasNext();)
  -		{
  -			ConnectionEventListener cel = (ConnectionEventListener) i.next();
  -			cel.connectionErrorOccurred(ce);
  -		}
  -	}
  -
   	ManagedConnectionFactory getManagedConnectionFactory()
   	{
   		Util.log("In OTMJCAManagedConnection.getManagedConnectionFactory");
  @@ -254,47 +254,75 @@
   	public void begin() throws ResourceException
   	{
   		Util.log("In OTMJCAManagedConnection.begin");
  -		try
  +		if (!isManaged())
   		{
  -			Transaction transaction = m_managedConnectionFactory.getKit().getTransaction(m_connection);
  -			Util.log("transaction is: " + transaction);
  -			transaction.begin();
  +			try
  +			{
  +				m_tx = m_managedConnectionFactory.getKit().getTransaction(m_connection);
  +				m_tx.begin();
  +				setManaged(true);
  +			}
  +			catch (TransactionException e)
  +			{
  +				throw new ResourceException(e.getMessage());
  +			}
   		}
  -		catch (TransactionException e)
  +		else
   		{
  -			throw new ResourceException(e.getMessage());
  +			throw new ResourceException("You probably called begin again without calling Commit
or Rollback, OTM does not support nested Local Transactions.");
   		}
   	}
   
   	public void commit() throws ResourceException
   	{
   		Util.log("In OTMJCAManagedConnection.commit");
  -		Transaction transaction = m_managedConnectionFactory.getKit().getTransaction(m_connection);
  -		try
  +		if (isManaged())
   		{
  -			Util.log("transaction is: " + transaction);
  -			transaction.commit();
  +			try
  +			{
  +				setManaged(false);
  +				m_tx.commit();
  +			}
  +			catch (TransactionException e)
  +			{
  +				m_tx.rollback();
  +				throw new ResourceException(e.getMessage());
  +			}
   		}
  -		catch (TransactionException e)
  +		else
   		{
  -			transaction.rollback();
  -			throw new ResourceException(e.getMessage());
  +			throw new ResourceException("Cannot call commit when you are not in a Local Transaction.");
   		}
   	}
   
   	public void rollback() throws ResourceException
   	{
   		Util.log("In OTMJCAManagedConnection.rollback");
  -		try
  +		if (isManaged())
   		{
  -			Transaction transaction = m_managedConnectionFactory.getKit().getTransaction(m_connection);
  -			Util.log("transaction is: " + transaction);
  -			transaction.rollback();
  +			try
  +			{
  +				m_tx.rollback();
  +				setManaged(false);
  +			}
  +			catch (TransactionException e)
  +			{
  +				throw new ResourceException(e.getMessage());
  +			}
   		}
  -		catch (TransactionException e)
  +		else
   		{
  -			throw new ResourceException(e.getMessage());
  +			throw new ResourceException("Cannot call rollback when you are not in a Local Transaction.");
   		}
   	}
   
  +	private boolean isManaged()
  +	{
  +		return m_managed;
  +	}
  +
  +	private void setManaged(boolean flag)
  +	{
  +		m_managed = flag;
  +	}
   }
  
  
  
  1.4       +15 -20    db-ojb/src/jca/org/apache/ojb/otm/connector/OTMJCAConnection.java
  
  Index: OTMJCAConnection.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/jca/org/apache/ojb/otm/connector/OTMJCAConnection.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- OTMJCAConnection.java	2 Jul 2003 01:22:28 -0000	1.3
  +++ OTMJCAConnection.java	2 Jul 2003 17:33:48 -0000	1.4
  @@ -86,91 +86,86 @@
   		this.m_managedConnection = mc;
   	}
   
  -	public OTMJCAManagedConnection getManagedConnection()
  -	{
  -		return m_managedConnection;
  -	}
  -
  -	public void setManagedConnection(OTMJCAManagedConnection managedConnection)
  +	void setManagedConnection(OTMJCAManagedConnection managedConnection)
   	{
   		m_managedConnection = managedConnection;
   	}
   
   	public void makePersistent(Object o) throws LockingException
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		m_managedConnection.getConnection().makePersistent(o);
   	}
   
   	public void deletePersistent(Object o) throws LockingException
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		m_managedConnection.getConnection().deletePersistent(o);
   	}
   
   	public void lockForWrite(Object o) throws LockingException
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		m_managedConnection.getConnection().lockForWrite(o);
   	}
   
   	public Object getObjectByIdentity(Identity identity) throws LockingException
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		return m_managedConnection.getConnection().getObjectByIdentity(identity);
   	}
   
   	public Object getObjectByIdentity(Identity identity, int i) throws LockingException
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		return m_managedConnection.getConnection().getObjectByIdentity(identity, i);
   	}
   
   	public Iterator getIteratorByQuery(Query query)
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		return m_managedConnection.getConnection().getIteratorByQuery(query);
   	}
   
   	public Iterator getIteratorByQuery(Query query, int i)
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		return m_managedConnection.getConnection().getIteratorByQuery(query, i);
   	}
   
   	public Identity getIdentity(Object o)
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		return m_managedConnection.getConnection().getIdentity(o);
   	}
   
   	public ClassDescriptor getDescriptorFor(Class aClass)
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		return m_managedConnection.getConnection().getDescriptorFor(aClass);
   	}
   
   	public EditingContext getEditingContext()
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		return m_managedConnection.getConnection().getEditingContext();
   	}
   
   	public void invalidate(Identity identity) throws LockingException
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		m_managedConnection.getConnection().invalidate(identity);
   	}
   
   	public EnhancedOQLQuery newOQLQuery()
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		return m_managedConnection.getConnection().newOQLQuery();
   	}
   
   	public int getCount(Query query)
   	{
  -		checkStatus();
  +		isValidUnderlyingConnection();
   		return m_managedConnection.getConnection().getCount(query);
   	}
   
  @@ -184,7 +179,7 @@
   		m_managedConnection = null;
   	}
   
  -	protected void checkStatus() throws OTMConnectionRuntimeException
  +	private void isValidUnderlyingConnection() throws OTMConnectionRuntimeException
   	{
   		if (m_closed)
   		{
  
  
  

Mime
View raw message