use of javax.transaction.NotSupportedException in project ovirt-engine by oVirt.
the class TransactionSupport method executeInNewTransaction.
/**
* Forces "REQUIRES_NEW" and executes given code in that scope
*/
public static <T> T executeInNewTransaction(TransactionMethod<T> code) {
T result = null;
Transaction transaction = null;
try {
TransactionManager tm = findTransactionManager();
// suspend existing if exists
transaction = tm.getTransaction();
if (transaction != null) {
transaction = tm.suspend();
}
// start new transaction
tm.begin();
Transaction newTransaction = tm.getTransaction();
// run the code
try {
result = code.runInTransaction();
} catch (RuntimeException rte) {
tm.rollback();
log.info("transaction rolled back");
throw rte;
} catch (Exception e) {
// code failed need to rollback
tm.rollback();
log.info("transaction rolled back");
log.error("executeInNewTransaction - Wrapping Exception: {} with RunTimeException", e.getClass().getName());
throw new RuntimeException("Failed executing code", e);
}
// commit or rollback according to state
if (needToRollback(newTransaction.getStatus())) {
tm.rollback();
} else {
tm.commit();
}
} catch (SystemException | NotSupportedException | HeuristicRollbackException | HeuristicMixedException | RollbackException | IllegalStateException | SecurityException e) {
throw new RuntimeException("Failed managing transaction", e);
} finally {
// check if we need to resume previous transaction
if (transaction != null) {
resume(transaction);
}
}
// and we are done...
return result;
}
use of javax.transaction.NotSupportedException in project webapp by elimu-ai.
the class GoogleCloudTextToSpeechHelper method synthesizeText.
public static byte[] synthesizeText(String text, Language language) throws NotSupportedException, IOException {
logger.info("synthesizeText");
byte[] byteArray = null;
logger.info("text: \"" + text + "\"");
if ((language != Language.BEN) && (language != Language.ENG) && (language != Language.FIL) && (language != Language.HIN)) {
throw new NotSupportedException("This language (" + language + ") is not yet supported: https://cloud.google.com/text-to-speech/docs/voices");
}
String languageCode = null;
if (language == Language.BEN) {
languageCode = "bn";
} else if (language == Language.ENG) {
languageCode = "en";
} else if (language == Language.FIL) {
languageCode = "fil";
} else if (language == Language.HIN) {
languageCode = "hi";
}
logger.info("languageCode: " + languageCode);
// For the Google Cloud TextToSpeechClient to work, an environment variable GOOGLE_APPLICATION_CREDENTIALS needs to exist.
// To enable this during development, download the JSON file from https://console.cloud.google.com/iam-admin/serviceaccounts
// and run the following command:
// export GOOGLE_APPLICATION_CREDENTIALS=/path/to/google-cloud-service-account-key.json
logger.info("System.getenv(\"GOOGLE_APPLICATION_CREDENTIALS\"): \"" + System.getenv("GOOGLE_APPLICATION_CREDENTIALS") + "\"");
TextToSpeechClient textToSpeechClient = TextToSpeechClient.create();
logger.info("textToSpeechClient: " + textToSpeechClient);
// Set the text input to be synthesized
SynthesisInput synthesisInput = SynthesisInput.newBuilder().setText(text).build();
logger.info("synthesisInput: " + synthesisInput);
// Build the voice request
VoiceSelectionParams voiceSelectionParams = VoiceSelectionParams.newBuilder().setLanguageCode(// TODO: fetch from Language enum
languageCode).setSsmlGender(SsmlVoiceGender.FEMALE).build();
logger.info("voiceSelectionParams: " + voiceSelectionParams);
// Select the type of audio file to be returned
AudioConfig audioConfig = AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
logger.info("audioConfig: " + audioConfig);
// Perform Text-to-Speech request
SynthesizeSpeechResponse synthesizeSpeechResponse = textToSpeechClient.synthesizeSpeech(synthesisInput, voiceSelectionParams, audioConfig);
logger.info("synthesizeSpeechResponse: " + synthesizeSpeechResponse);
// Get the audio contents from the response
ByteString audioContentsByteString = synthesizeSpeechResponse.getAudioContent();
// Convert to byte array
byteArray = audioContentsByteString.toByteArray();
return byteArray;
}
use of javax.transaction.NotSupportedException in project kylo by Teradata.
the class JcrMetadataAccess method read.
public <R> R read(Credentials creds, MetadataCommand<R> cmd) {
ActiveSession session = activeSession.get();
if (session == null) {
try {
activeSession.set(new ActiveSession(this.repository.login(creds)));
TransactionManager txnMgr = this.txnLookup.getTransactionManager();
try {
txnMgr.begin();
return execute(creds, cmd);
} finally {
try {
txnMgr.rollback();
} catch (SystemException e) {
log.error("Failed to rollback transaction", e);
}
activeSession.get().session.refresh(false);
activeSession.get().session.logout();
activeSession.remove();
}
} catch (SystemException | NotSupportedException | RepositoryException e) {
throw new MetadataAccessException("Failure accessing the metadata store", e);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new MetadataExecutionException(e);
}
} else {
try {
return cmd.execute();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new MetadataExecutionException(e);
}
}
}
use of javax.transaction.NotSupportedException in project wildfly by wildfly.
the class DatabaseTimerPersistence method shouldRun.
@Override
public boolean shouldRun(TimerImpl timer) {
final ContextTransactionManager tm = ContextTransactionManager.getInstance();
if (!allowExecution) {
// timers never execute on this node
return false;
}
String loadTimer = sql.getProperty(UPDATE_RUNNING);
Connection connection = null;
PreparedStatement statement = null;
try {
tm.begin();
try {
connection = dataSource.getConnection();
statement = connection.prepareStatement(loadTimer);
statement.setString(1, TimerState.IN_TIMEOUT.name());
setNodeName(TimerState.IN_TIMEOUT, statement, 2);
statement.setString(3, timer.getId());
statement.setString(4, TimerState.IN_TIMEOUT.name());
statement.setString(5, TimerState.RETRY_TIMEOUT.name());
if (timer.getNextExpiration() == null) {
statement.setTimestamp(6, null);
} else {
statement.setTimestamp(6, timestamp(timer.getNextExpiration()));
}
} catch (SQLException e) {
try {
tm.rollback();
} catch (Exception ee) {
EjbLogger.EJB3_TIMER_LOGGER.timerUpdateFailedAndRollbackNotPossible(ee);
}
// fix for WFLY-10130
EjbLogger.EJB3_TIMER_LOGGER.exceptionCheckingIfTimerShouldRun(timer, e);
return false;
}
int affected = statement.executeUpdate();
tm.commit();
return affected == 1;
} catch (SQLException | SystemException | SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
// failed to update the DB
try {
tm.rollback();
} catch (IllegalStateException | SecurityException | SystemException rbe) {
EjbLogger.EJB3_TIMER_LOGGER.timerUpdateFailedAndRollbackNotPossible(rbe);
}
EjbLogger.EJB3_TIMER_LOGGER.debugf(e, "Timer %s not running due to exception ", timer);
return false;
} catch (NotSupportedException e) {
// happen from tm.begin, no rollback necessary
EjbLogger.EJB3_TIMER_LOGGER.timerNotRunning(e, timer);
return false;
} finally {
safeClose(statement);
safeClose(connection);
}
}
use of javax.transaction.NotSupportedException in project datanucleus-core by datanucleus.
the class JTATransactionImpl method begin.
/**
* JDO spec "16.1.3 Stateless Session Bean with Bean Managed Transactions": "acquiring a PM without beginning a UserTransaction results
* in the PM being able to manage transaction boundaries via begin, commit, and rollback methods on JDO Transaction.
* The PM will automatically begin the User-Transaction during Transaction.begin and automatically commit the UserTransaction during Transaction.commit"
*/
public void begin() {
joinTransaction();
if (joinStatus != JoinStatus.NO_TXN) {
throw new NucleusTransactionException("JTA Transaction is already active");
}
UserTransaction utx;
try {
Context ctx = new InitialContext();
if (// TODO If JBoss starts using the JavaEE standard location, we need to remove this alternative location
JBOSS_SERVER) {
// JBoss unfortunately doesn't always provide UserTransaction at the JavaEE standard location, see e.g. http://docs.jboss.org/admin-devel/Chap4.html
try {
utx = (UserTransaction) ctx.lookup("UserTransaction");
} catch (NamingException e) {
// Fallback to standard location
utx = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
}
} else {
utx = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
}
} catch (NamingException e) {
throw ec.getApiAdapter().getUserExceptionForException("Failed to obtain JTA UserTransaction", e);
}
try {
utx.begin();
} catch (NotSupportedException | SystemException e) {
throw ec.getApiAdapter().getUserExceptionForException("Failed to begin JTA UserTransaction", e);
}
joinTransaction();
if (joinStatus != JoinStatus.JOINED) {
throw new NucleusTransactionException("Cannot join an auto started JTA UserTransaction");
}
userTransaction = utx;
}
Aggregations