Codota Logo
ExtendedConnectionDataSourceProxy
Code IndexAdd Codota to your IDE (free)

How to use
ExtendedConnectionDataSourceProxy
in
org.springframework.batch.item.database

Best Java code snippets using org.springframework.batch.item.database.ExtendedConnectionDataSourceProxy (Showing top 20 results out of 315)

  • Add the Codota plugin to your IDE and get smart completions
private void myMethod () {
DateTime d =
  • Codota Iconnew DateTime()
  • Codota IconDateTimeFormatter formatter;String text;formatter.parseDateTime(text)
  • Codota IconObject instant;new DateTime(instant)
  • Smart code suggestions by Codota
}
origin: spring-projects/spring-batch

private Connection initConnection(String username, String password) throws SQLException {
  if (closeSuppressedConnection != null) {
    if (!borrowedConnection) {
      borrowedConnection = true;
      return closeSuppressedConnection;
    }
  }
  Connection target;
  if (username != null) {
    target = dataSource.getConnection(username, password);
  }
  else {
    target = dataSource.getConnection();
  }
  return getCloseSuppressingConnectionProxy(target);
}
origin: spring-projects/spring-batch

@Override
public Connection getConnection() throws SQLException {
  synchronized (this.connectionMonitor) {
    return initConnection(null, null);
  }
}
origin: spring-projects/spring-batch

/**
 * @see SmartDataSource
 */
@Override
public boolean shouldClose(Connection connection) {
  boolean shouldClose = !isCloseSuppressionActive(connection);
  if (borrowedConnection && closeSuppressedConnection.equals(connection)) {
    borrowedConnection = false;
  }
  return shouldClose;
}
origin: spring-projects/spring-batch

@Test(expected = IllegalArgumentException.class)
public void delegateIsRequired() throws Exception {
  ExtendedConnectionDataSourceProxy tested = new ExtendedConnectionDataSourceProxy(null);
  tested.afterPropertiesSet();
}
origin: spring-projects/spring-batch

@Test
public void testOperationWithDirectCloseCall() throws SQLException {
  Connection con = mock(Connection.class);
  DataSource ds = mock(DataSource.class);
  when(ds.getConnection()).thenReturn(con); // con1
  con.close();
  when(ds.getConnection()).thenReturn(con); // con2
  con.close();
  final ExtendedConnectionDataSourceProxy csds = new ExtendedConnectionDataSourceProxy(ds);
  Connection con1 = csds.getConnection();
  csds.startCloseSuppression(con1);
  Connection con1_1 = csds.getConnection();
  assertSame("should be same connection", con1_1, con1);
  con1_1.close(); // no mock call for this - should be suppressed
  Connection con1_2 = csds.getConnection();
  assertSame("should be same connection", con1_2, con1);
  Connection con2 = csds.getConnection();
  assertNotSame("shouldn't be same connection", con2, con1);
  csds.stopCloseSuppression(con1);
  assertTrue("should be able to close connection", csds.shouldClose(con1));
  con1_1 = null;
  con1_2 = null;
  con1.close();
  assertTrue("should be able to close connection", csds.shouldClose(con2));
  con2.close();
}
origin: spring-projects/spring-batch

final ExtendedConnectionDataSourceProxy csds = new ExtendedConnectionDataSourceProxy();
csds.setDataSource(ds);
PlatformTransactionManager tm = new DataSourceTransactionManager(csds);
TransactionTemplate tt = new TransactionTemplate(tm);
csds.startCloseSuppression(connection);
tt.execute(new TransactionCallback<Void>() {
  @Override
csds.stopCloseSuppression(connection);
DataSourceUtils.releaseConnection(connection, csds);
template.queryForList("select egg from bar");
origin: spring-projects/spring-batch

@Test
public void unwrapForSupportedInterface() throws Exception {
  DataSourceStub ds = new DataSourceStub();
  ExtendedConnectionDataSourceProxy tested = new ExtendedConnectionDataSourceProxy(ds);
  assertTrue(tested.isWrapperFor(Supported.class));
  assertEquals(ds, tested.unwrap(Supported.class));
}
origin: spring-projects/spring-batch

/**
 * Close the cursor and database connection. Make call to cleanupOnClose so sub classes can cleanup
 * any resources they have allocated.
 */
@Override
protected void doClose() throws Exception {
  initialized = false;
  JdbcUtils.closeResultSet(this.rs);
  rs = null;
  cleanupOnClose();
  if(this.con != null) {
    this.con.setAutoCommit(this.initialConnectionAutoCommit);
  }
  if (useSharedExtendedConnection && dataSource instanceof ExtendedConnectionDataSourceProxy) {
    ((ExtendedConnectionDataSourceProxy)dataSource).stopCloseSuppression(this.con);
    if (!TransactionSynchronizationManager.isActualTransactionActive()) {
      DataSourceUtils.releaseConnection(con, dataSource);
    }
  }
  else {
    JdbcUtils.closeConnection(this.con);
  }
}
origin: spring-projects/spring-batch

protected void initializeConnection() {
  Assert.state(getDataSource() != null, "DataSource must not be null.");
  try {
    if (useSharedExtendedConnection) {
      if (!(getDataSource() instanceof ExtendedConnectionDataSourceProxy)) {
        throw new InvalidDataAccessApiUsageException(
            "You must use a ExtendedConnectionDataSourceProxy for the dataSource when " +
            "useSharedExtendedConnection is set to true.");
      }
      this.con = DataSourceUtils.getConnection(dataSource);
      ((ExtendedConnectionDataSourceProxy)dataSource).startCloseSuppression(this.con);
    }
    else {
      this.con = dataSource.getConnection();
    }
    this.initialConnectionAutoCommit = this.con.getAutoCommit();
    if (this.connectionAutoCommit != null && this.con.getAutoCommit() != this.connectionAutoCommit) {
      this.con.setAutoCommit(this.connectionAutoCommit);
    }
  }
  catch (SQLException se) {
    close();
    throw getExceptionTranslator().translate("Executing query", getSql(), se);
  }
}
origin: spring-projects/spring-batch

@Test
public void testUsesCurrentTransaction() throws Exception {
  DataSource ds = mock(DataSource.class);
  Connection con = mock(Connection.class);
  when(con.getAutoCommit()).thenReturn(false);
  PreparedStatement ps = mock(PreparedStatement.class);
  when(con.prepareStatement("select foo from bar", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
      ResultSet.HOLD_CURSORS_OVER_COMMIT)).thenReturn(ps);
  when(ds.getConnection()).thenReturn(con);
  when(ds.getConnection()).thenReturn(con);
  con.commit();
  PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
  TransactionTemplate tt = new TransactionTemplate(tm);
  final JdbcCursorItemReader<String> reader = new JdbcCursorItemReader<>();
  reader.setDataSource(new ExtendedConnectionDataSourceProxy(ds));
  reader.setUseSharedExtendedConnection(true);
  reader.setSql("select foo from bar");
  final ExecutionContext ec = new ExecutionContext();
  tt.execute(
      new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
          reader.open(ec);
          reader.close();
          return null;
        }
      });
}

origin: spring-projects/spring-batch

final ExtendedConnectionDataSourceProxy csds = new ExtendedConnectionDataSourceProxy(ds);
Connection con1 = csds.getConnection();
Connection con2 = csds.getConnection();
assertNotSame("shouldn't be the same connection", con1, con2);
assertTrue("should be able to close connection", csds.shouldClose(con1));
con1.close();
assertTrue("should be able to close connection", csds.shouldClose(con2));
con2.close();
Connection con3 = csds.getConnection();
csds.startCloseSuppression(con3);
Connection con3_1 = csds.getConnection();
assertSame("should be same connection", con3_1, con3);
assertFalse("should not be able to close connection", csds.shouldClose(con3));
Connection con3_2 = csds.getConnection();
assertSame("should be same connection", con3_2, con3);
Connection con4 = csds.getConnection();
assertNotSame("shouldn't be same connection", con4, con3);
csds.stopCloseSuppression(con3);
assertTrue("should be able to close connection", csds.shouldClose(con3));
con3_1 = null;
con3_2 = null;
con3.close();
assertTrue("should be able to close connection", csds.shouldClose(con4));
con4.close();
origin: spring-projects/spring-batch

@Test
public void unwrapForSmartDataSource() throws Exception {
  ExtendedConnectionDataSourceProxy tested = new ExtendedConnectionDataSourceProxy(new DataSourceStub());
  assertTrue(tested.isWrapperFor(DataSource.class));
  assertEquals(tested, tested.unwrap(DataSource.class));
  assertTrue(tested.isWrapperFor(SmartDataSource.class));
  assertEquals(tested, tested.unwrap(SmartDataSource.class));
}
origin: apache/servicemix-bundles

/**
 * Close the cursor and database connection. Make call to cleanupOnClose so sub classes can cleanup
 * any resources they have allocated.
 */
@Override
protected void doClose() throws Exception {
  initialized = false;
  JdbcUtils.closeResultSet(this.rs);
  rs = null;
  cleanupOnClose();
  if(this.con != null) {
    this.con.setAutoCommit(this.initialConnectionAutoCommit);
  }
  if (useSharedExtendedConnection && dataSource instanceof ExtendedConnectionDataSourceProxy) {
    ((ExtendedConnectionDataSourceProxy)dataSource).stopCloseSuppression(this.con);
    if (!TransactionSynchronizationManager.isActualTransactionActive()) {
      DataSourceUtils.releaseConnection(con, dataSource);
    }
  }
  else {
    JdbcUtils.closeConnection(this.con);
  }
}
origin: apache/servicemix-bundles

protected void initializeConnection() {
  Assert.state(getDataSource() != null, "DataSource must not be null.");
  try {
    if (useSharedExtendedConnection) {
      if (!(getDataSource() instanceof ExtendedConnectionDataSourceProxy)) {
        throw new InvalidDataAccessApiUsageException(
            "You must use a ExtendedConnectionDataSourceProxy for the dataSource when " +
            "useSharedExtendedConnection is set to true.");
      }
      this.con = DataSourceUtils.getConnection(dataSource);
      ((ExtendedConnectionDataSourceProxy)dataSource).startCloseSuppression(this.con);
    }
    else {
      this.con = dataSource.getConnection();
    }
    this.initialConnectionAutoCommit = this.con.getAutoCommit();
    if (this.connectionAutoCommit != null && this.con.getAutoCommit() != this.connectionAutoCommit) {
      this.con.setAutoCommit(this.connectionAutoCommit);
    }
  }
  catch (SQLException se) {
    close();
    throw getExceptionTranslator().translate("Executing query", getSql(), se);
  }
}
origin: spring-projects/spring-batch

TransactionTemplate tt = new TransactionTemplate(tm);
final StoredProcedureItemReader<String> reader = new StoredProcedureItemReader<>();
reader.setDataSource(new ExtendedConnectionDataSourceProxy(ds));
reader.setUseSharedExtendedConnection(true);
reader.setProcedureName("foo_bar");
origin: spring-projects/spring-batch

@Test
public void unwrapForUnsupportedInterface() throws Exception {
  ExtendedConnectionDataSourceProxy tested = new ExtendedConnectionDataSourceProxy(new DataSourceStub());
  assertFalse(tested.isWrapperFor(Unsupported.class));
  try {
    tested.unwrap(Unsupported.class);
    fail();
  }
  catch (SQLException expected) {
    //			this would be the correct behavior in a Java6-only recursive implementation
    //			assertEquals(DataSourceStub.UNWRAP_ERROR_MESSAGE, expected.getMessage());
    assertEquals("Unsupported class " + Unsupported.class.getSimpleName(), expected.getMessage());
  }
}
origin: spring-projects/spring-batch

@Override
public Connection getConnection(String username, String password) throws SQLException {
  synchronized (this.connectionMonitor) {
    return initConnection(username, password);
  }
}
origin: spring-projects/spring-batch

private boolean completeCloseCall(Connection connection) {
  if (borrowedConnection && closeSuppressedConnection.equals(connection)) {
    borrowedConnection = false;
  }
  return isCloseSuppressionActive(connection);
}
origin: apache/servicemix-bundles

private Connection initConnection(String username, String password) throws SQLException {
  if (closeSuppressedConnection != null) {
    if (!borrowedConnection) {
      borrowedConnection = true;
      return closeSuppressedConnection;
    }
  }
  Connection target;
  if (username != null) {
    target = dataSource.getConnection(username, password);
  }
  else {
    target = dataSource.getConnection();
  }
  return getCloseSuppressingConnectionProxy(target);
}
origin: apache/servicemix-bundles

@Override
public Connection getConnection(String username, String password) throws SQLException {
  synchronized (this.connectionMonitor) {
    return initConnection(username, password);
  }
}
org.springframework.batch.item.databaseExtendedConnectionDataSourceProxy

Javadoc

Implementation of SmartDataSource that is capable of keeping a single JDBC Connection which is NOT closed after each use even if Connection#close() is called. The connection can be kept open over multiple transactions when used together with any of Spring's org.springframework.transaction.PlatformTransactionManagerimplementations.

Loosely based on the SingleConnectionDataSource implementation in Spring Core. Intended to be used with the JdbcCursorItemReader to provide a connection that remains open across transaction boundaries, It remains open for the life of the cursor, and can be shared with the main transaction of the rest of the step processing.

Once close suppression has been turned on for a connection, it will be returned for the first #getConnection() call. Any subsequent calls to #getConnection() will retrieve a new connection from the wrapped DataSource until the DataSourceUtils queries whether the connection should be closed or not by calling #shouldClose(Connection) for the close-suppressed Connection. At that point the cycle starts over again, and the next #getConnection() call will have the Connection that is being close-suppressed returned. This allows the use of the close-suppressed Connection to be the main Connection for an extended data access process. The close suppression is turned off by calling #stopCloseSuppression(Connection).

This class is not multi-threading capable.

The connection returned will be a close-suppressing proxy instead of the physical Connection. Be aware that you will not be able to cast this to a native OracleConnection or the like anymore; you'd be required to use java.sql.Connection#unwrap(Class).

Most used methods

  • startCloseSuppression
  • stopCloseSuppression
  • getCloseSuppressingConnectionProxy
    Wrap the given Connection with a proxy that delegates every method call to it but suppresses close c
  • initConnection
  • isCloseSuppressionActive
    Return the status of close suppression being activated for a given Connection
  • <init>
    Constructor that takes as a parameter with the DataSource to be wrapped.
  • afterPropertiesSet
  • getConnection
  • isWrapperFor
    Performs only a 'shallow' non-recursive check of self's and delegate's class to retain Java 5 compat
  • setDataSource
    Setter for the DataSource that is to be wrapped.
  • shouldClose
  • unwrap
    Returns either self or delegate (in this order) if one of them can be cast to supplied parameter cla
  • shouldClose,
  • unwrap

Popular in Java

  • Reading from database using SQL prepared statement
  • setScale (BigDecimal)
  • getResourceAsStream (ClassLoader)
    Returns a stream for the resource with the specified name. See #getResource(String) for a descriptio
  • getExternalFilesDir (Context)
  • BigDecimal (java.math)
    An immutable arbitrary-precision signed decimal.A value is represented by an arbitrary-precision "un
  • URL (java.net)
    A Uniform Resource Locator that identifies the location of an Internet resource as specified by RFC
  • Format (java.text)
    The base class for all formats. This is an abstract base class which specifies the protocol for clas
  • HashMap (java.util)
    HashMap is an implementation of Map. All optional operations are supported.All elements are permitte
  • JOptionPane (javax.swing)
  • Table (org.hibernate.mapping)
    A relational table
Codota Logo
  • Products

    Search for Java codeSearch for JavaScript codeEnterprise
  • IDE Plugins

    IntelliJ IDEAWebStormAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogCodota Academy Plugin user guide Terms of usePrivacy policyJava Code IndexJavascript Code Index
Get Codota for your IDE now