View Javadoc
1   package org.synchronoss.cpo.jdbc;
2   
3   /*-
4    * [[
5    * jdbc
6    * ==
7    * Copyright (C) 2003 - 2026 Exaxis LLC, Synchronoss Technologies Inc
8    * ==
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ]]
23   */
24  
25  import java.io.Serializable;
26  import java.sql.Connection;
27  import java.sql.DatabaseMetaData;
28  import org.synchronoss.cpo.core.CpoException;
29  
30  /**
31   * The capabilities of the database behind a {@link JdbcCpoAdapter}, probed once from {@link
32   * DatabaseMetaData} when the adapter is constructed. Immutable; add a field here (and probe it in
33   * {@link #probe(JdbcConnectionStrategy)}) when the adapter needs a new capability test.
34   *
35   * @author david berry
36   */
37  class JdbcDatabaseCapabilities implements Serializable {
38  
39    /** Version Id for this class. */
40    private static final long serialVersionUID = 1L;
41  
42    private final boolean batchUpdatesSupported;
43  
44    private JdbcDatabaseCapabilities(boolean batchUpdatesSupported) {
45      this.batchUpdatesSupported = batchUpdatesSupported;
46    }
47  
48    /**
49     * Probes the database's capabilities using a short-lived read connection.
50     *
51     * @param connectionStrategy The strategy used to obtain and release the probe connection
52     * @return The probed capabilities
53     * @throws CpoException The database metadata could not be retrieved
54     */
55    static JdbcDatabaseCapabilities probe(JdbcConnectionStrategy connectionStrategy)
56        throws CpoException {
57      Connection c = null;
58      try {
59        c = connectionStrategy.getReadConnection();
60        DatabaseMetaData dmd = c.getMetaData();
61  
62        // do all the tests here
63        return new JdbcDatabaseCapabilities(dmd.supportsBatchUpdates());
64      } catch (Throwable t) {
65        throw new CpoException("Could Not Retrieve Database Metadata", t);
66      } finally {
67        // terminate the read-only transaction before pooling the connection (see existsBean)
68        connectionStrategy.rollbackLocalConnection(c);
69        connectionStrategy.closeLocalConnection(c);
70      }
71    }
72  
73    /**
74     * Are batch updates supported
75     *
76     * @return true if batch updates are supported
77     */
78    boolean batchUpdatesSupported() {
79      return batchUpdatesSupported;
80    }
81  }