Search in sources :

Example 41 with NotSupportedException

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;
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) TransactionRolledbackLocalException(javax.ejb.TransactionRolledbackLocalException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException)

Example 42 with NotSupportedException

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;
}
Also used : ByteString(com.google.protobuf.ByteString) SynthesizeSpeechResponse(com.google.cloud.texttospeech.v1beta1.SynthesizeSpeechResponse) AudioConfig(com.google.cloud.texttospeech.v1beta1.AudioConfig) ByteString(com.google.protobuf.ByteString) SynthesisInput(com.google.cloud.texttospeech.v1beta1.SynthesisInput) NotSupportedException(javax.transaction.NotSupportedException) TextToSpeechClient(com.google.cloud.texttospeech.v1beta1.TextToSpeechClient) VoiceSelectionParams(com.google.cloud.texttospeech.v1beta1.VoiceSelectionParams)

Example 43 with NotSupportedException

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);
        }
    }
}
Also used : SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) MetadataAccessException(com.thinkbiganalytics.metadata.api.MetadataAccessException) RepositoryException(javax.jcr.RepositoryException) MetadataExecutionException(com.thinkbiganalytics.metadata.api.MetadataExecutionException) NotSupportedException(javax.transaction.NotSupportedException) MetadataAccessException(com.thinkbiganalytics.metadata.api.MetadataAccessException) RepositoryException(javax.jcr.RepositoryException) LockException(javax.jcr.lock.LockException) MetadataLockException(com.thinkbiganalytics.metadata.modeshape.support.MetadataLockException) MetadataExecutionException(com.thinkbiganalytics.metadata.api.MetadataExecutionException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException)

Example 44 with NotSupportedException

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);
    }
}
Also used : ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) ParseException(java.text.ParseException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) StartException(org.jboss.msc.service.StartException) IOException(java.io.IOException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException)

Example 45 with NotSupportedException

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;
}
Also used : NucleusTransactionException(org.datanucleus.transaction.NucleusTransactionException) UserTransaction(javax.transaction.UserTransaction) InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) SystemException(javax.transaction.SystemException) NamingException(javax.naming.NamingException) NotSupportedException(javax.transaction.NotSupportedException) InitialContext(javax.naming.InitialContext)

Aggregations

NotSupportedException (javax.transaction.NotSupportedException)53 SystemException (javax.transaction.SystemException)42 RollbackException (javax.transaction.RollbackException)22 HeuristicMixedException (javax.transaction.HeuristicMixedException)20 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)20 Transaction (javax.transaction.Transaction)17 UserTransaction (javax.transaction.UserTransaction)11 TransactionManager (javax.transaction.TransactionManager)10 SQLException (java.sql.SQLException)8 NamingException (javax.naming.NamingException)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 InvalidTransactionException (javax.transaction.InvalidTransactionException)5 Connection (java.sql.Connection)4 GeneralException (org.apache.openjpa.util.GeneralException)4 File (java.io.File)3 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2