Search in sources :

Example 16 with JDBCException

use of org.hibernate.JDBCException in project hibernate-orm by hibernate.

the class CustomInsertSQLWithIdentityColumnTest method testBadInsertionFails.

@Test
public void testBadInsertionFails() {
    Session session = openSession();
    session.beginTransaction();
    Organization org = new Organization("hola!");
    try {
        session.save(org);
        session.delete(org);
        fail("expecting bad custom insert statement to fail");
    } catch (JDBCException e) {
    // expected failure
    }
    session.getTransaction().rollback();
    session.close();
}
Also used : Organization(org.hibernate.test.sql.hand.Organization) JDBCException(org.hibernate.JDBCException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 17 with JDBCException

use of org.hibernate.JDBCException in project hibernate-orm by hibernate.

the class StoredProcedureTest method testInParametersNullnessPassingInNamedQueriesViaHints.

@Test
@SuppressWarnings("unchecked")
public void testInParametersNullnessPassingInNamedQueriesViaHints() {
    Session session = openSession();
    session.beginTransaction();
    // similar to #testInParametersNotSet and #testInParametersNotSetPass in terms of testing
    // support for specifying whether to pass NULL argument values or not.  This version tests
    // named procedure support via hints.
    // first a fixture - this execution should fail
    {
        ProcedureCall query = session.getNamedProcedureCall("findUserRangeNoNullPassing");
        query.getParameterRegistration(2).bindValue(2);
        try {
            query.getOutputs();
            fail("Expecting failure due to missing parameter bind");
        } catch (JDBCException ignore) {
        }
    }
    // here we enable NULL passing via hint through a named parameter
    {
        ProcedureCall query = session.getNamedProcedureCall("findUserRangeNamedNullPassing");
        query.getParameterRegistration("secondArg").bindValue(2);
        query.getOutputs();
    }
    // here we enable NULL passing via hint through a named parameter
    {
        ProcedureCall query = session.getNamedProcedureCall("findUserRangeOrdinalNullPassing");
        query.getParameterRegistration(2).bindValue(2);
        query.getOutputs();
    }
    session.getTransaction().commit();
    session.close();
}
Also used : ProcedureCall(org.hibernate.procedure.ProcedureCall) JDBCException(org.hibernate.JDBCException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 18 with JDBCException

use of org.hibernate.JDBCException in project kylo by Teradata.

the class FeedRestController method saveDraftFeed.

@POST
@Path("/{feedId}/versions/draft/entity")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED })
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Creates or saves a feed as draft version.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the feed metadata.", response = NifiFeed.class), @ApiResponse(code = 500, message = "An internal error occurred.", response = RestResponseStatus.class) })
@Nonnull
public Response saveDraftFeed(@Nonnull final FeedMetadata feedMetadata) {
    try {
        FeedMetadata update = getMetadataService().saveDraftFeed(feedMetadata);
        NifiFeed feed = new NifiFeed(update, null);
        feed.setSuccess(true);
        return Response.ok(feed).build();
    } catch (DuplicateFeedNameException e) {
        log.info("Failed to create a new feed due to another feed having the same category/feed name: " + feedMetadata.getCategoryAndFeedDisplayName());
        // Create an error message
        String msg = "A feed already exists in the category \"" + e.getCategoryName() + "\" with name name \"" + e.getFeedName() + "\"";
        // Add error message to feed
        NifiFeed feed = new NifiFeed(feedMetadata, null);
        feed.addErrorMessage(msg);
        feed.setSuccess(false);
        return Response.status(Status.CONFLICT).entity(feed).build();
    } catch (NifiConnectionException e) {
        throw e;
    } catch (Exception e) {
        log.error("Failed to create a new feed.", e);
        // Create an error message
        String msg = (e.getMessage() != null) ? "Error creating Feed: " + e.getMessage() : "An unknown error occurred while saving the feed.";
        if (e.getCause() instanceof JDBCException) {
            msg += ". " + ((JDBCException) e).getSQLException();
        }
        // Add error message to feed
        NifiFeed feed = new NifiFeed(feedMetadata, null);
        feed.addErrorMessage(msg);
        feed.setSuccess(false);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(feed).build();
    }
}
Also used : JDBCException(org.hibernate.JDBCException) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) DuplicateFeedNameException(com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException) NifiFeed(com.thinkbiganalytics.feedmgr.rest.model.NifiFeed) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) FeedCleanupTimeoutException(com.thinkbiganalytics.feedmgr.service.FeedCleanupTimeoutException) FeedCleanupFailedException(com.thinkbiganalytics.feedmgr.service.FeedCleanupFailedException) WebApplicationException(javax.ws.rs.WebApplicationException) AccessDeniedException(java.nio.file.AccessDeniedException) DeployFeedException(com.thinkbiganalytics.feedmgr.service.feed.DeployFeedException) DuplicateFeedNameException(com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException) IOException(java.io.IOException) FeedCurrentlyRunningException(com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedCurrentlyRunningException) ClientErrorException(javax.ws.rs.ClientErrorException) JDBCException(org.hibernate.JDBCException) FeedHistoryDataReindexingNotEnabledException(com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedHistoryDataReindexingNotEnabledException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) AccessControlException(java.security.AccessControlException) DataAccessException(org.springframework.dao.DataAccessException) VersionNotFoundException(com.thinkbiganalytics.metadata.api.versioning.VersionNotFoundException) FeedNotFoundException(com.thinkbiganalytics.metadata.api.feed.FeedNotFoundException) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Nonnull(javax.annotation.Nonnull) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 19 with JDBCException

use of org.hibernate.JDBCException in project kylo by Teradata.

the class FeedRestController method createDraftFeed.

@POST
@Path("/draft/entity")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED })
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Creates a new feed as draft version.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the feed metadata.", response = NifiFeed.class), @ApiResponse(code = 500, message = "An internal error occurred.", response = RestResponseStatus.class) })
@Nonnull
public Response createDraftFeed(@Nonnull final FeedMetadata feedMetadata) {
    try {
        FeedMetadata update = getMetadataService().saveDraftFeed(feedMetadata);
        NifiFeed feed = new NifiFeed(update, null);
        feed.setSuccess(true);
        return Response.ok(feed).build();
    } catch (DuplicateFeedNameException e) {
        log.info("Failed to create a new feed due to another feed having the same category/feed name: " + feedMetadata.getCategoryAndFeedDisplayName());
        // Create an error message
        String msg = "A feed already exists in the category \"" + e.getCategoryName() + "\" with name name \"" + e.getFeedName() + "\"";
        // Add error message to feed
        NifiFeed feed = new NifiFeed(feedMetadata, null);
        feed.addErrorMessage(msg);
        feed.setSuccess(false);
        return Response.status(Status.CONFLICT).entity(feed).build();
    } catch (NifiConnectionException e) {
        throw e;
    } catch (Exception e) {
        log.error("Failed to create a new feed.", e);
        // Create an error message
        String msg = (e.getMessage() != null) ? "Error creating Feed: " + e.getMessage() : "An unknown error occurred while saving the feed.";
        if (e.getCause() instanceof JDBCException) {
            msg += ". " + ((JDBCException) e).getSQLException();
        }
        // Add error message to feed
        NifiFeed feed = new NifiFeed(feedMetadata, null);
        feed.addErrorMessage(msg);
        feed.setSuccess(false);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(feed).build();
    }
}
Also used : JDBCException(org.hibernate.JDBCException) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) DuplicateFeedNameException(com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException) NifiFeed(com.thinkbiganalytics.feedmgr.rest.model.NifiFeed) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) FeedCleanupTimeoutException(com.thinkbiganalytics.feedmgr.service.FeedCleanupTimeoutException) FeedCleanupFailedException(com.thinkbiganalytics.feedmgr.service.FeedCleanupFailedException) WebApplicationException(javax.ws.rs.WebApplicationException) AccessDeniedException(java.nio.file.AccessDeniedException) DeployFeedException(com.thinkbiganalytics.feedmgr.service.feed.DeployFeedException) DuplicateFeedNameException(com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException) IOException(java.io.IOException) FeedCurrentlyRunningException(com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedCurrentlyRunningException) ClientErrorException(javax.ws.rs.ClientErrorException) JDBCException(org.hibernate.JDBCException) FeedHistoryDataReindexingNotEnabledException(com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedHistoryDataReindexingNotEnabledException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) AccessControlException(java.security.AccessControlException) DataAccessException(org.springframework.dao.DataAccessException) VersionNotFoundException(com.thinkbiganalytics.metadata.api.versioning.VersionNotFoundException) FeedNotFoundException(com.thinkbiganalytics.metadata.api.feed.FeedNotFoundException) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Nonnull(javax.annotation.Nonnull) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 20 with JDBCException

use of org.hibernate.JDBCException in project jo-client-platform by jo-source.

the class HibernateExceptionDecoratorImpl method decorate.

private Throwable decorate(final Throwable exception, final Throwable rootException) {
    if (exception instanceof ConstraintViolationException) {
        final ConstraintViolationException constViolationException = (ConstraintViolationException) exception;
        final String message = constViolationException.getMessage();
        final String constraintName = constViolationException.getConstraintName();
        final String userBaseMessage = Messages.getString("HibernateExceptionDecoratorImpl.database_constraint_violated");
        final String userMessage;
        if (!EmptyCheck.isEmpty(constraintName)) {
            userMessage = userBaseMessage.replace("%1", "'" + constraintName + "'");
        } else {
            final SQLException sqlException = constViolationException.getSQLException();
            if (sqlException != null) {
                if (!EmptyCheck.isEmpty(sqlException.getLocalizedMessage())) {
                    userMessage = sqlException.getLocalizedMessage();
                } else if (!EmptyCheck.isEmpty(sqlException.getMessage())) {
                    userMessage = sqlException.getMessage();
                } else {
                    userMessage = userBaseMessage.replace("%1", "");
                }
            } else {
                userMessage = userBaseMessage.replace("%1", "");
            }
        }
        return new ExecutableCheckException(null, message, userMessage);
    } else if (exception instanceof OptimisticLockException && exception.getCause() instanceof StaleObjectStateException) {
        return getStaleBeanException((StaleObjectStateException) exception);
    } else if (exception instanceof UnresolvableObjectException) {
        return getDeletedBeanException((UnresolvableObjectException) exception);
    } else if (exception instanceof JDBCException && !excludeJDBCExceptionDecoration((JDBCException) exception)) {
        return decorateJDBCException((JDBCException) exception);
    } else if (exception instanceof InvocationTargetException && ((InvocationTargetException) exception).getTargetException() != null) {
        return decorate(((InvocationTargetException) exception).getTargetException(), rootException);
    } else if (exception.getCause() != null) {
        return decorate(exception.getCause(), rootException);
    }
    return rootException;
}
Also used : JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) OptimisticLockException(javax.persistence.OptimisticLockException) ExecutableCheckException(org.jowidgets.cap.common.api.exception.ExecutableCheckException) StaleObjectStateException(org.hibernate.StaleObjectStateException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

JDBCException (org.hibernate.JDBCException)22 SQLException (java.sql.SQLException)13 Test (org.junit.Test)11 Session (org.hibernate.Session)9 PreparedStatement (java.sql.PreparedStatement)7 StaleObjectStateException (org.hibernate.StaleObjectStateException)5 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)3 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)2 NifiFeed (com.thinkbiganalytics.feedmgr.rest.model.NifiFeed)2 FeedCleanupFailedException (com.thinkbiganalytics.feedmgr.service.FeedCleanupFailedException)2 FeedCleanupTimeoutException (com.thinkbiganalytics.feedmgr.service.FeedCleanupTimeoutException)2 DeployFeedException (com.thinkbiganalytics.feedmgr.service.feed.DeployFeedException)2 DuplicateFeedNameException (com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException)2 FeedCurrentlyRunningException (com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedCurrentlyRunningException)2 FeedHistoryDataReindexingNotEnabledException (com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedHistoryDataReindexingNotEnabledException)2 FeedNotFoundException (com.thinkbiganalytics.metadata.api.feed.FeedNotFoundException)2 VersionNotFoundException (com.thinkbiganalytics.metadata.api.versioning.VersionNotFoundException)2 NifiClientRuntimeException (com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException)2 NifiConnectionException (com.thinkbiganalytics.nifi.rest.client.NifiConnectionException)2