Я разрабатываю приложение на основе Griddb в Java. Время от времени транзакции не сбои из -за сетевых тайм -аутов или переходных ошибок. com.toshiba.mwcloud.gs.*;
import org.apache.logging.log4j.logmanager;
import org.apache.logging.log4j.logger; < /p>
Logmanager.getlogger (transactionretryexample.class); < /p>
public static void main(String[] args) {
GridStore store = null;
try {
// Connect to GridDB
Properties props = new Properties();
props.setProperty("host", "127.0.0.1");
props.setProperty("port", "31999");
props.setProperty("clusterName", "myCluster");
props.setProperty("username", "admin");
props.setProperty("password", "admin_pass");
store = GridStoreFactory.getInstance().getStore(props);
logger.info("Connected to GridDB.");
} catch (GSException e) {
logger.error("Connection failed:", e);
return;
}
String containerName = "TransactionTest";
final int maxRetries = 3;
int attempt = 0;
boolean success = false;
while (attempt < maxRetries && !success) {
attempt++;
try {
store.beginTransaction();
Container container = store.getContainer(containerName);
Object[] row = {"sampleKey", "sampleValue"};
container.put(row);
store.commitTransaction();
logger.info("Transaction committed on attempt " + attempt);
success = true;
} catch (GSException e) {
logger.warn("Transaction failed on attempt " + attempt + ": " + e.getMessage());
try {
store.abortTransaction();
} catch (GSException abortEx) {
logger.error("Failed to abort transaction: ", abortEx);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
logger.error("Sleep interrupted:", ie);
}
}
}
if (!success) {
logger.error("Transaction failed after " + maxRetries + " attempts.");
}
}
< /code>
} `< /p>
Я внедрил цикл повторной попытки с 3 попытками. сетевые тайм -ауты. Тем не менее, я не уверен, какие исключения являются действительно временными и безопасными для повторения, в сравнении с теми, какие из них указывают на невыкупаемость неудачи. /> В многопоточной среде, какие рекомендуемые методы, чтобы избежать дублирования операций при повторении транзакций (например, стратегии идентичности, уникальные ключи, компенсационные действия)? практики) не полностью описаны. Я был бы признателен за понимание или закономерности, которые другие успешно использовали.
Подробнее здесь: https://stackoverflow.com/questions/797 ... ailed-logg