@SuppressWarnings("unchecked") @Test public void testTemplateExecuteInteractionCallback() throws ResourceException, SQLException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Interaction interaction = mock(Interaction.class); InteractionCallback<Object> interactionCallback = mock(InteractionCallback.class); given(connectionFactory.getConnection()).willReturn(connection); given(connection.createInteraction()).willReturn(interaction); given(interactionCallback.doInInteraction(interaction,connectionFactory)).willReturn(new Object()); CciTemplate ct = new CciTemplate(connectionFactory); ct.execute(interactionCallback); verify(interactionCallback).doInInteraction(interaction,connectionFactory); verify(interaction).close(); verify(connection).close(); }
given(connection.getLocalTransaction()).willReturn(localTransaction); given(connection.createInteraction()).willReturn(interaction); given(interaction.execute(interactionSpec, record, record)).willReturn(true); given(connection.getLocalTransaction()).willReturn(localTransaction); verify(interaction).close(); verify(localTransaction).commit(); verify(connection).close();
/** * Close the given Connection. * @param con the Connection to close */ protected void closeConnection(Connection con) { try { con.close(); } catch (Throwable ex) { logger.warn("Could not close shared CCI Connection", ex); } }
@Override protected void doRollback(DefaultTransactionStatus status) { CciLocalTransactionObject txObject = (CciLocalTransactionObject) status.getTransaction(); Connection con = txObject.getConnectionHolder().getConnection(); if (status.isDebug()) { logger.debug("Rolling back CCI local transaction on Connection [" + con + "]"); } try { con.getLocalTransaction().rollback(); } catch (LocalTransactionException ex) { throw new TransactionSystemException("Could not roll back CCI local transaction", ex); } catch (ResourceException ex) { throw new TransactionSystemException("Unexpected failure on rollback of CCI local transaction", ex); } }
if (ConnectionFactory.class.isInstance(cf)) { final Connection connection = ConnectionFactory.class.cast(cf).getConnection(); connection.getMetaData(); connection.close();
@Override @Nullable public <T> T execute(final InteractionCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); return execute((ConnectionCallback<T>) (connection, connectionFactory) -> { Interaction interaction = connection.createInteraction(); try { return action.doInInteraction(interaction, connectionFactory); } finally { closeInteraction(interaction); } }); }
/** * If logging is turned on and the CCI implementation supports meta data then display connection info. */ protected void buildConnectLog(AbstractSession session) { try { // Log connection information. if (session.shouldLog(SessionLog.CONFIG, SessionLog.CONNECTION)) {// Avoid printing if no logging required. ConnectionMetaData metaData = getCCIConnection().getMetaData(); Object[] args = { metaData.getUserName(), metaData.getEISProductName(), metaData.getEISProductVersion(), Helper.cr(), "\t" }; session.log(SessionLog.CONFIG, SessionLog.CONNECTION, "connected_user_database", args, this); } } catch (ResourceException exception) { // Some databases do not support metadata, ignore exception. session.warning("JDBC_driver_does_not_support_meta_data", SessionLog.CONNECTION); } }
/** * Close the given Connection. * @param con the Connection to close */ protected void closeConnection(Connection con) { try { con.close(); } catch (Throwable ex) { logger.warn("Could not close shared CCI Connection", ex); } }
@Override protected void doCommit(DefaultTransactionStatus status) { CciLocalTransactionObject txObject = (CciLocalTransactionObject) status.getTransaction(); Connection con = txObject.getConnectionHolder().getConnection(); if (status.isDebug()) { logger.debug("Committing CCI local transaction on Connection [" + con + "]"); } try { con.getLocalTransaction().commit(); } catch (LocalTransactionException ex) { throw new TransactionSystemException("Could not commit CCI local transaction", ex); } catch (ResourceException ex) { throw new TransactionSystemException("Unexpected failure on commit of CCI local transaction", ex); } }
if (cf instanceof ConnectionFactory) { final Connection connection = ((ConnectionFactory) cf).getConnection(); connection.getMetaData(); connection.close();
@Override @Nullable public <T> T execute(final InteractionCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); return execute((ConnectionCallback<T>) (connection, connectionFactory) -> { Interaction interaction = connection.createInteraction(); try { return action.doInInteraction(interaction, connectionFactory); } finally { closeInteraction(interaction); } }); }
/** * If logging is turned on and the CCI implementation supports meta data then display connection info. */ protected void buildConnectLog(AbstractSession session) { try { // Log connection information. if (session.shouldLog(SessionLog.CONFIG, SessionLog.CONNECTION)) {// Avoid printing if no logging required. ConnectionMetaData metaData = getCCIConnection().getMetaData(); Object[] args = { metaData.getUserName(), metaData.getEISProductName(), metaData.getEISProductVersion(), Helper.cr(), "\t" }; session.log(SessionLog.CONFIG, SessionLog.CONNECTION, "connected_user_database", args, this); } } catch (ResourceException exception) { // Some databases do not support metadata, ignore exception. session.warning("JDBC_driver_does_not_support_meta_data", SessionLog.CONNECTION); } }
@Test public void testTemplateExecuteInputTrueTrue() throws ResourceException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Interaction interaction = mock(Interaction.class); Record inputOutputRecord = mock(Record.class); InteractionSpec interactionSpec = mock(InteractionSpec.class); given(connectionFactory.getConnection()).willReturn(connection); given(connection.createInteraction()).willReturn(interaction); given(interaction.execute(interactionSpec, inputOutputRecord, inputOutputRecord)).willReturn(true); CciTemplate ct = new CciTemplate(connectionFactory); ct.execute(interactionSpec, inputOutputRecord, inputOutputRecord); verify(interaction).execute(interactionSpec, inputOutputRecord, inputOutputRecord); verify(interaction).close(); verify(connection).close(); }
/** * Actually close the given Connection, obtained from the given ConnectionFactory. * Same as {@link #releaseConnection}, but throwing the original ResourceException. * <p>Directly accessed by {@link TransactionAwareConnectionFactoryProxy}. * @param con the Connection to close if necessary * (if this is {@code null}, the call will be ignored) * @param cf the ConnectionFactory that the Connection was obtained from * (can be {@code null}) * @throws ResourceException if thrown by JCA CCI methods * @see #doGetConnection */ public static void doReleaseConnection(@Nullable Connection con, @Nullable ConnectionFactory cf) throws ResourceException { if (con == null || isConnectionTransactional(con, cf)) { return; } con.close(); }
given(connection.getLocalTransaction()).willReturn(localTransaction); given(connection.createInteraction()).willReturn(interaction); given(interaction.execute(interactionSpec, record, record)).willReturn(true); given(connection.getLocalTransaction()).willReturn(localTransaction); verify(interaction).close(); verify(localTransaction).rollback(); verify(connection).close();
@Override protected void doCommit(DefaultTransactionStatus status) { CciLocalTransactionObject txObject = (CciLocalTransactionObject) status.getTransaction(); Connection con = txObject.getConnectionHolder().getConnection(); if (status.isDebug()) { logger.debug("Committing CCI local transaction on Connection [" + con + "]"); } try { con.getLocalTransaction().commit(); } catch (LocalTransactionException ex) { throw new TransactionSystemException("Could not commit CCI local transaction", ex); } catch (ResourceException ex) { throw new TransactionSystemException("Unexpected failure on commit of CCI local transaction", ex); } }
@Override @Nullable public <T> T execute(final InteractionCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); return execute((ConnectionCallback<T>) (connection, connectionFactory) -> { Interaction interaction = connection.createInteraction(); try { return action.doInInteraction(interaction, connectionFactory); } finally { closeInteraction(interaction); } }); }
/** * If logging is turned on and the CCI implementation supports meta data then display connection info. */ @Override protected void buildConnectLog(AbstractSession session) { try { // Log connection information. if (session.shouldLog(SessionLog.CONFIG, SessionLog.CONNECTION)) {// Avoid printing if no logging required. ConnectionMetaData metaData = getCCIConnection().getMetaData(); Object[] args = { metaData.getUserName(), metaData.getEISProductName(), metaData.getEISProductVersion(), Helper.cr(), "\t" }; session.log(SessionLog.CONFIG, SessionLog.CONNECTION, "connected_user_database", args, this); } } catch (ResourceException exception) { // Some databases do not support metadata, ignore exception. session.warning("JDBC_driver_does_not_support_meta_data", SessionLog.CONNECTION); } }
@Test public void testSimpleRecordOperationWithInputOutputRecord() throws ResourceException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Interaction interaction = mock(Interaction.class); Record inputOutputRecord = mock(Record.class); InteractionSpec interactionSpec = mock(InteractionSpec.class); SimpleRecordOperation query = new SimpleRecordOperation(connectionFactory, interactionSpec); given(connectionFactory.getConnection()).willReturn(connection); given(connection.createInteraction()).willReturn(interaction); given(interaction.execute(interactionSpec, inputOutputRecord, inputOutputRecord)).willReturn(true); query.execute(inputOutputRecord, inputOutputRecord); verify(interaction).execute(interactionSpec, inputOutputRecord, inputOutputRecord); verify(interaction).close(); verify(connection).close(); }
/** * Actually close the given Connection, obtained from the given ConnectionFactory. * Same as {@link #releaseConnection}, but throwing the original ResourceException. * <p>Directly accessed by {@link TransactionAwareConnectionFactoryProxy}. * @param con the Connection to close if necessary * (if this is {@code null}, the call will be ignored) * @param cf the ConnectionFactory that the Connection was obtained from * (can be {@code null}) * @throws ResourceException if thrown by JCA CCI methods * @see #doGetConnection */ public static void doReleaseConnection(@Nullable Connection con, @Nullable ConnectionFactory cf) throws ResourceException { if (con == null || isConnectionTransactional(con, cf)) { return; } con.close(); }