/** {@inheritDoc} */ @Override public CacheJdbcBlobStore<K, V> create() { CacheJdbcBlobStore<K, V> store = new CacheJdbcBlobStore<>(); store.setInitSchema(initSchema); store.setConnectionUrl(connUrl); store.setCreateTableQuery(createTblQry); store.setLoadQuery(loadQry); store.setUpdateQuery(updateQry); store.setInsertQuery(insertQry); store.setDeleteQuery(delQry); store.setUser(user); store.setPassword(passwd); if (dataSrc != null) store.setDataSource(dataSrc); else if (dataSrcBean != null) { if (appCtx == null) throw new IgniteException("Spring application context resource is not injected."); IgniteSpringHelper spring; try { spring = IgniteComponentType.SPRING.create(false); DataSource data = spring.loadBeanFromAppContext(appCtx, dataSrcBean); store.setDataSource(data); } catch (IgniteCheckedException ignored) { throw new IgniteException("Failed to load bean in application context [beanName=" + dataSrcBean + ", igniteConfig=" + appCtx + ']'); } } return store; }
/** {@inheritDoc} */ @Override public void sessionEnd(boolean commit) { init(); Transaction tx = transaction(); Map<String, Connection> props = session().properties(); Connection conn = props.remove(ATTR_CONN); if (conn != null) { try { if (commit) conn.commit(); else conn.rollback(); } catch (SQLException e) { throw new CacheWriterException("Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e); } finally { closeConnection(conn); } } if (log.isDebugEnabled()) log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']'); }
/** {@inheritDoc} */ @Override public void delete(Object key) { init(); Transaction tx = transaction(); if (log.isDebugEnabled()) log.debug("Store remove [key=" + key + ", tx=" + tx + ']'); Connection conn = null; PreparedStatement stmt = null; try { conn = connection(tx); stmt = conn.prepareStatement(delQry); stmt.setObject(1, toBytes(key)); stmt.executeUpdate(); } catch (IgniteCheckedException | SQLException e) { throw new CacheWriterException("Failed to remove object: " + key, e); } finally { end(tx, conn, stmt); } }
/** * @param tx Cache transaction. * @return Connection. * @throws SQLException In case of error. */ private Connection connection(@Nullable Transaction tx) throws SQLException { if (tx != null) { Map<String, Connection> props = session().properties(); Connection conn = props.get(ATTR_CONN); if (conn == null) { conn = openConnection(false); // Store connection in session properties, so it can be accessed // for other operations on the same transaction. props.put(ATTR_CONN, conn); } return conn; } // Transaction can be null in case of simple load operation. else return openConnection(true); }
/** {@inheritDoc} */ @Override protected CacheJdbcBlobStore<Object, Object> store() { return new CacheJdbcBlobStore<>(); } }
conn = openConnection(false); U.closeQuiet(stmt); closeConnection(conn);
/** * @return Current transaction. */ @Nullable private Transaction transaction() { CacheStoreSession ses = session(); return ses != null ? ses.transaction() : null; }
/** * Closes allocated resources depending on transaction status. * * @param tx Active transaction, if any. * @param conn Allocated connection. * @param st Created statement, */ private void end(@Nullable Transaction tx, Connection conn, Statement st) { U.closeQuiet(st); if (tx == null) // Close connection right away if there is no transaction. closeConnection(conn); }
/** {@inheritDoc} */ @Override protected CacheStore getStore() { return new CacheJdbcBlobStore(); }
/** * @param tx Cache transaction. * @return Connection. * @throws SQLException In case of error. */ private Connection connection(@Nullable Transaction tx) throws SQLException { if (tx != null) { Map<String, Connection> props = session().properties(); Connection conn = props.get(ATTR_CONN); if (conn == null) { conn = openConnection(false); // Store connection in session properties, so it can be accessed // for other operations on the same transaction. props.put(ATTR_CONN, conn); } return conn; } // Transaction can be null in case of simple load operation. else return openConnection(true); }
conn = openConnection(false); U.closeQuiet(stmt); closeConnection(conn);
/** * @return Current transaction. */ @Nullable private Transaction transaction() { CacheStoreSession ses = session(); return ses != null ? ses.transaction() : null; }
/** * Closes allocated resources depending on transaction status. * * @param tx Active transaction, if any. * @param conn Allocated connection. * @param st Created statement, */ private void end(@Nullable Transaction tx, Connection conn, Statement st) { U.closeQuiet(st); if (tx == null) // Close connection right away if there is no transaction. closeConnection(conn); }
/** {@inheritDoc} */ @Override public CacheJdbcBlobStore<K, V> create() { CacheJdbcBlobStore<K, V> store = new CacheJdbcBlobStore<>(); store.setInitSchema(initSchema); store.setConnectionUrl(connUrl); store.setCreateTableQuery(createTblQry); store.setLoadQuery(loadQry); store.setUpdateQuery(updateQry); store.setInsertQuery(insertQry); store.setDeleteQuery(delQry); store.setUser(user); store.setPassword(passwd); if (dataSrc != null) store.setDataSource(dataSrc); else if (dataSrcBean != null) { if (appCtx == null) throw new IgniteException("Spring application context resource is not injected."); IgniteSpringHelper spring; try { spring = IgniteComponentType.SPRING.create(false); DataSource data = spring.loadBeanFromAppContext(appCtx, dataSrcBean); store.setDataSource(data); } catch (IgniteCheckedException ignored) { throw new IgniteException("Failed to load bean in application context [beanName=" + dataSrcBean + ", igniteConfig=" + appCtx + ']'); } } return store; }
/** {@inheritDoc} */ @Override public V load(K key) { init(); Transaction tx = transaction(); if (log.isDebugEnabled()) log.debug("Store load [key=" + key + ", tx=" + tx + ']'); Connection conn = null; PreparedStatement stmt = null; try { conn = connection(tx); stmt = conn.prepareStatement(loadQry); stmt.setObject(1, toBytes(key)); ResultSet rs = stmt.executeQuery(); if (rs.next()) return fromBytes(rs.getBytes(2)); } catch (IgniteCheckedException | SQLException e) { throw new CacheLoaderException("Failed to load object: " + key, e); } finally { end(tx, conn, stmt); } return null; }
/** {@inheritDoc} */ @Override public void sessionEnd(boolean commit) { init(); Transaction tx = transaction(); Map<String, Connection> props = session().properties(); Connection conn = props.remove(ATTR_CONN); if (conn != null) { try { if (commit) conn.commit(); else conn.rollback(); } catch (SQLException e) { throw new CacheWriterException("Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e); } finally { closeConnection(conn); } } if (log.isDebugEnabled()) log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']'); }
@Override public CacheStore create() { try { CacheStore<Integer, String> store = new CacheJdbcBlobStore<>(); Field f = store.getClass().getDeclaredField("testMode"); f.setAccessible(true); f.set(store, true); return store; } catch (NoSuchFieldException | IllegalAccessException e) { throw new IgniteException(e); } } }
init(); Transaction tx = transaction(); conn = connection(tx); stmt.setObject(1, toBytes(val)); stmt.setObject(2, toBytes(key)); stmt.setObject(1, toBytes(key)); stmt.setObject(2, toBytes(val)); end(tx, conn, stmt);
/** {@inheritDoc} */ @Override public void delete(Object key) { init(); Transaction tx = transaction(); if (log.isDebugEnabled()) log.debug("Store remove [key=" + key + ", tx=" + tx + ']'); Connection conn = null; PreparedStatement stmt = null; try { conn = connection(tx); stmt = conn.prepareStatement(delQry); stmt.setObject(1, toBytes(key)); stmt.executeUpdate(); } catch (IgniteCheckedException | SQLException e) { throw new CacheWriterException("Failed to remove object: " + key, e); } finally { end(tx, conn, stmt); } }
/** {@inheritDoc} */ @SuppressWarnings({"RedundantTypeArguments"}) @Override public V load(K key) { init(); Transaction tx = transaction(); if (log.isDebugEnabled()) log.debug("Store load [key=" + key + ", tx=" + tx + ']'); Connection conn = null; PreparedStatement stmt = null; try { conn = connection(tx); stmt = conn.prepareStatement(loadQry); stmt.setObject(1, toBytes(key)); ResultSet rs = stmt.executeQuery(); if (rs.next()) return fromBytes(rs.getBytes(2)); } catch (IgniteCheckedException | SQLException e) { throw new CacheLoaderException("Failed to load object: " + key, e); } finally { end(tx, conn, stmt); } return null; }