use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheHibernatePersonStore method delete.
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "JpaQueryApiInspection" })
@Override
public void delete(Object key) {
System.out.println(">>> Store delete [key=" + key + ']');
Session hibSes = ses.attachment();
try {
hibSes.createQuery("delete " + Person.class.getSimpleName() + " where key = :key").setParameter("key", key).executeUpdate();
} catch (HibernateException e) {
throw new CacheWriterException("Failed to remove value from cache store [key=" + key + ']', e);
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheJdbcPersonStore method write.
/**
* {@inheritDoc}
*/
@Override
public void write(Cache.Entry<? extends Long, ? extends Person> entry) {
Long key = entry.getKey();
Person val = entry.getValue();
System.out.println(">>> Store write [key=" + key + ", val=" + val + ']');
try {
Connection conn = ses.attachment();
int updated;
// Some databases would allow these to be done in one 'upsert' operation.
try (PreparedStatement st = conn.prepareStatement("update PERSON set first_name = ?, last_name = ? where id = ?")) {
st.setString(1, val.firstName);
st.setString(2, val.lastName);
st.setLong(3, val.id);
updated = st.executeUpdate();
}
// If update failed, try to insert.
if (updated == 0) {
try (PreparedStatement st = conn.prepareStatement("insert into PERSON (id, first_name, last_name) values (?, ?, ?)")) {
st.setLong(1, val.id);
st.setString(2, val.firstName);
st.setString(3, val.lastName);
st.executeUpdate();
}
}
} catch (SQLException e) {
throw new CacheWriterException("Failed to write object [key=" + key + ", val=" + val + ']', e);
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheJdbcBlobStore method sessionEnd.
/**
* {@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 + ']');
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheJdbcBlobStore method write.
/**
* {@inheritDoc}
*/
@Override
public void write(Cache.Entry<? extends K, ? extends V> entry) {
init();
Transaction tx = transaction();
K key = entry.getKey();
V val = entry.getValue();
if (log.isDebugEnabled())
log.debug(S.toString("Store put", "key", key, true, "val", val, true, "tx", tx, false));
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = connection(tx);
stmt = conn.prepareStatement(updateQry);
stmt.setObject(1, toBytes(val));
stmt.setObject(2, toBytes(key));
if (stmt.executeUpdate() == 0) {
stmt.close();
stmt = conn.prepareStatement(insertQry);
stmt.setObject(1, toBytes(key));
stmt.setObject(2, toBytes(val));
stmt.executeUpdate();
}
} catch (IgniteCheckedException | SQLException e) {
throw new CacheWriterException("Failed to put object [key=" + key + ", val=" + val + ']', e);
} finally {
end(tx, conn, stmt);
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheAbstractJdbcStore method sessionEnd.
/**
* {@inheritDoc}
*/
@Override
public void sessionEnd(boolean commit) throws CacheWriterException {
CacheStoreSession ses = session();
Transaction tx = ses.transaction();
if (tx != null) {
Map<String, Connection> sesProps = ses.properties();
Connection conn = sesProps.get(ATTR_CONN_PROP);
if (conn != null) {
sesProps.remove(ATTR_CONN_PROP);
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 {
U.closeQuiet(conn);
}
}
if (log.isDebugEnabled())
log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']');
}
}
Aggregations