Java Code Examples for org.springframework.dao.support.PersistenceExceptionTranslator

The following examples show how to use org.springframework.dao.support.PersistenceExceptionTranslator. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public Object invoke(MethodInvocation mi) throws Throwable {

	// Invoke the method potentially returning a reactive type
	Object m = mi.proceed();

	PersistenceExceptionTranslator translator = getPersistenceExceptionTranslator();
	if (translator == null) {
		return m;
	} else {
		// Add the translation. Nothing will happen if no-one subscribe the reactive result.
		Function<RuntimeException, Throwable> errorMappingFunction =
			t -> t instanceof DataAccessException ? t : DataAccessUtils.translateIfNecessary(t, translator);
		if (m instanceof Mono) {
			return ((Mono<?>) m).onErrorMap(RuntimeException.class, errorMappingFunction);
		} else if (m instanceof Flux) {
			return ((Flux<?>) m).onErrorMap(RuntimeException.class, errorMappingFunction);
		} else {
			return m;
		}
	}
}
 
Example 2
/**
 * Actually create the EntityManager proxy.
 * @param rawEm raw EntityManager
 * @param emIfc the (potentially vendor-specific) EntityManager
 * interface to proxy, or {@code null} for default detection of all interfaces
 * @param cl the ClassLoader to use for proxy creation (maybe {@code null})
 * @param exceptionTranslator the PersistenceException translator to use
 * @param jta whether to create a JTA-aware EntityManager
 * (or {@code null} if not known in advance)
 * @param containerManaged whether to follow container-managed EntityManager
 * or application-managed EntityManager semantics
 * @param synchronizedWithTransaction whether to automatically join ongoing
 * transactions (according to the JPA 2.1 SynchronizationType rules)
 * @return the EntityManager proxy
 */
private static EntityManager createProxy(
		EntityManager rawEm, @Nullable Class<? extends EntityManager> emIfc, @Nullable ClassLoader cl,
		@Nullable PersistenceExceptionTranslator exceptionTranslator, @Nullable Boolean jta,
		boolean containerManaged, boolean synchronizedWithTransaction) {

	Assert.notNull(rawEm, "EntityManager must not be null");
	Set<Class<?>> ifcs = new LinkedHashSet<>();
	if (emIfc != null) {
		ifcs.add(emIfc);
	}
	else {
		ifcs.addAll(ClassUtils.getAllInterfacesForClassAsSet(rawEm.getClass(), cl));
	}
	ifcs.add(EntityManagerProxy.class);
	return (EntityManager) Proxy.newProxyInstance(
			(cl != null ? cl : ExtendedEntityManagerCreator.class.getClassLoader()),
			ClassUtils.toClassArray(ifcs),
			new ExtendedEntityManagerInvocationHandler(
					rawEm, exceptionTranslator, jta, containerManaged, synchronizedWithTransaction));
}
 
Example 3
/**
 * Actually create the EntityManager proxy.
 * @param rawEm raw EntityManager
 * @param emIfc the (potentially vendor-specific) EntityManager
 * interface to proxy, or {@code null} for default detection of all interfaces
 * @param cl the ClassLoader to use for proxy creation (maybe {@code null})
 * @param exceptionTranslator the PersistenceException translator to use
 * @param jta whether to create a JTA-aware EntityManager
 * (or {@code null} if not known in advance)
 * @param containerManaged whether to follow container-managed EntityManager
 * or application-managed EntityManager semantics
 * @param synchronizedWithTransaction whether to automatically join ongoing
 * transactions (according to the JPA 2.1 SynchronizationType rules)
 * @return the EntityManager proxy
 */
private static EntityManager createProxy(
		EntityManager rawEm, @Nullable Class<? extends EntityManager> emIfc, @Nullable ClassLoader cl,
		@Nullable PersistenceExceptionTranslator exceptionTranslator, @Nullable Boolean jta,
		boolean containerManaged, boolean synchronizedWithTransaction) {

	Assert.notNull(rawEm, "EntityManager must not be null");
	Set<Class<?>> ifcs = new LinkedHashSet<>();
	if (emIfc != null) {
		ifcs.add(emIfc);
	}
	else {
		ifcs.addAll(ClassUtils.getAllInterfacesForClassAsSet(rawEm.getClass(), cl));
	}
	ifcs.add(EntityManagerProxy.class);
	return (EntityManager) Proxy.newProxyInstance(
			(cl != null ? cl : ExtendedEntityManagerCreator.class.getClassLoader()),
			ClassUtils.toClassArray(ifcs),
			new ExtendedEntityManagerInvocationHandler(
					rawEm, exceptionTranslator, jta, containerManaged, synchronizedWithTransaction));
}
 
Example 4
/**
 * Actually create the EntityManager proxy.
 * @param rawEm raw EntityManager
 * @param emIfc the (potentially vendor-specific) EntityManager
 * interface to proxy, or {@code null} for default detection of all interfaces
 * @param cl the ClassLoader to use for proxy creation (maybe {@code null})
 * @param exceptionTranslator the PersistenceException translator to use
 * @param jta whether to create a JTA-aware EntityManager
 * (or {@code null} if not known in advance)
 * @param containerManaged whether to follow container-managed EntityManager
 * or application-managed EntityManager semantics
 * @param synchronizedWithTransaction whether to automatically join ongoing
 * transactions (according to the JPA 2.1 SynchronizationType rules)
 * @return the EntityManager proxy
 */
private static EntityManager createProxy(
		EntityManager rawEm, Class<? extends EntityManager> emIfc, ClassLoader cl,
		PersistenceExceptionTranslator exceptionTranslator, Boolean jta,
		boolean containerManaged, boolean synchronizedWithTransaction) {

	Assert.notNull(rawEm, "EntityManager must not be null");
	Set<Class<?>> ifcs = new LinkedHashSet<Class<?>>();
	if (emIfc != null) {
		ifcs.add(emIfc);
	}
	else {
		ifcs.addAll(ClassUtils.getAllInterfacesForClassAsSet(rawEm.getClass(), cl));
	}
	ifcs.add(EntityManagerProxy.class);
	return (EntityManager) Proxy.newProxyInstance(
			(cl != null ? cl : ExtendedEntityManagerCreator.class.getClassLoader()),
			ifcs.toArray(new Class<?>[ifcs.size()]),
			new ExtendedEntityManagerInvocationHandler(
					rawEm, exceptionTranslator, jta, containerManaged, synchronizedWithTransaction));
}
 
Example 5
Source Project: tcc-transaction   Source File: CustomSqlSessionTemplate.java    License: Apache License 2.0 6 votes vote down vote up
public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
                                PersistenceExceptionTranslator exceptionTranslator) {

    super(sqlSessionFactory, executorType, exceptionTranslator);

    this.sqlSessionFactory = sqlSessionFactory;
    this.executorType = executorType;
    this.exceptionTranslator = exceptionTranslator;

    this.sqlSessionProxy = (SqlSession) newProxyInstance(
            SqlSessionFactory.class.getClassLoader(),
            new Class[] { SqlSession.class },
            new SqlSessionInterceptor());

    this.defaultTargetSqlSessionFactory = sqlSessionFactory;
}
 
Example 6
/**
 * Actually create the EntityManager proxy.
 * @param rawEm raw EntityManager
 * @param emIfc the (potentially vendor-specific) EntityManager
 * interface to proxy, or {@code null} for default detection of all interfaces
 * @param cl the ClassLoader to use for proxy creation (maybe {@code null})
 * @param exceptionTranslator the PersistenceException translator to use
 * @param jta whether to create a JTA-aware EntityManager
 * (or {@code null} if not known in advance)
 * @param containerManaged whether to follow container-managed EntityManager
 * or application-managed EntityManager semantics
 * @param synchronizedWithTransaction whether to automatically join ongoing
 * transactions (according to the JPA 2.1 SynchronizationType rules)
 * @return the EntityManager proxy
 */
private static EntityManager createProxy(
		EntityManager rawEm, Class<? extends EntityManager> emIfc, ClassLoader cl,
		PersistenceExceptionTranslator exceptionTranslator, Boolean jta,
		boolean containerManaged, boolean synchronizedWithTransaction) {

	Assert.notNull(rawEm, "EntityManager must not be null");
	Set<Class<?>> ifcs = new LinkedHashSet<Class<?>>();
	if (emIfc != null) {
		ifcs.add(emIfc);
	}
	else {
		ifcs.addAll(ClassUtils.getAllInterfacesForClassAsSet(rawEm.getClass(), cl));
	}
	ifcs.add(EntityManagerProxy.class);
	return (EntityManager) Proxy.newProxyInstance(
			(cl != null ? cl : ExtendedEntityManagerCreator.class.getClassLoader()),
			ifcs.toArray(new Class<?>[ifcs.size()]),
			new ExtendedEntityManagerInvocationHandler(
					rawEm, exceptionTranslator, jta, containerManaged, synchronizedWithTransaction));
}
 
Example 7
Source Project: galaxy   Source File: CustomSqlSessionTemplate.java    License: Apache License 2.0 6 votes vote down vote up
public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
                                PersistenceExceptionTranslator exceptionTranslator) {

    super(sqlSessionFactory, executorType, exceptionTranslator);

    this.sqlSessionFactory = sqlSessionFactory;
    this.executorType = executorType;
    this.exceptionTranslator = exceptionTranslator;

    this.sqlSessionProxy = (SqlSession) newProxyInstance(
            SqlSessionFactory.class.getClassLoader(),
            new Class[] { SqlSession.class },
            new SqlSessionInterceptor());

    this.defaultTargetSqlSessionFactory = sqlSessionFactory;
}
 
Example 8
PersistenceExceptionTranslator getPersistenceExceptionTranslator() {

		PersistenceExceptionTranslator translator = this.persistenceExceptionTranslator;
		if (translator == null) {
			synchronized (this) {
				translator = this.persistenceExceptionTranslator;
				if (translator == null) {
					this.persistenceExceptionTranslator = detectPersistenceExceptionTranslators();
					translator = this.persistenceExceptionTranslator;
				}
			}
		}
		return translator;
	}
 
Example 9
/**
 * Detect all PersistenceExceptionTranslators in the given BeanFactory.
 *
 * @return a chained PersistenceExceptionTranslator, combining all
 * PersistenceExceptionTranslators found in the factory
 * @see ChainedPersistenceExceptionTranslator
 */
private PersistenceExceptionTranslator detectPersistenceExceptionTranslators() {
	// Find all translators, being careful not to activate FactoryBeans.
	Map<String, PersistenceExceptionTranslator> pets = BeanFactoryUtils.beansOfTypeIncludingAncestors(
		beanFactory, PersistenceExceptionTranslator.class, false, false);
	ChainedPersistenceExceptionTranslator cpet = new ChainedPersistenceExceptionTranslator();
	pets.values().forEach(cpet::addDelegate);
	return cpet;
}
 
Example 10
private ExtendedEntityManagerInvocationHandler(EntityManager target,
		@Nullable PersistenceExceptionTranslator exceptionTranslator, @Nullable Boolean jta,
		boolean containerManaged, boolean synchronizedWithTransaction) {

	this.target = target;
	this.exceptionTranslator = exceptionTranslator;
	this.jta = (jta != null ? jta : isJtaEntityManager());
	this.containerManaged = containerManaged;
	this.synchronizedWithTransaction = synchronizedWithTransaction;
}
 
Example 11
public ExtendedEntityManagerSynchronization(
		EntityManager em, @Nullable PersistenceExceptionTranslator exceptionTranslator) {

	super(new EntityManagerHolder(em), em);
	this.entityManager = em;
	this.exceptionTranslator = exceptionTranslator;
}
 
Example 12
protected void doTestExceptionTranslationWithDialectFound(PersistenceExceptionTranslator pet) throws Exception {
	RuntimeException in1 = new RuntimeException("in1");
	PersistenceException in2 = new PersistenceException();
	assertNull("No translation here", pet.translateExceptionIfPossible(in1));
	DataAccessException dex = pet.translateExceptionIfPossible(in2);
	assertNotNull(dex);
	assertSame(in2, dex.getCause());
}
 
Example 13
@Override
public boolean matches(Class<?> clazz) {
	if (TransactionalProxy.class.isAssignableFrom(clazz) ||
			PlatformTransactionManager.class.isAssignableFrom(clazz) ||
			PersistenceExceptionTranslator.class.isAssignableFrom(clazz)) {
		return false;
	}
	TransactionAttributeSource tas = getTransactionAttributeSource();
	return (tas == null || tas.isCandidateClass(clazz));
}
 
Example 14
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
 * @param repositoryAnnotationType the annotation type to check for
 */
public PersistenceExceptionTranslationAdvisor(
		PersistenceExceptionTranslator persistenceExceptionTranslator,
		Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example 15
@Override
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	if (AnnotationUtils.findAnnotation(pf.getTargetClass(), Repository.class) != null) {
		DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
		bf.registerBeanDefinition("peti", new RootBeanDefinition(PersistenceExceptionTranslationInterceptor.class));
		bf.registerSingleton("pet", pet);
		pf.addAdvice((PersistenceExceptionTranslationInterceptor) bf.getBean("peti"));
	}
}
 
Example 16
private ExtendedEntityManagerInvocationHandler(EntityManager target,
		@Nullable PersistenceExceptionTranslator exceptionTranslator, @Nullable Boolean jta,
		boolean containerManaged, boolean synchronizedWithTransaction) {

	this.target = target;
	this.exceptionTranslator = exceptionTranslator;
	this.jta = (jta != null ? jta : isJtaEntityManager());
	this.containerManaged = containerManaged;
	this.synchronizedWithTransaction = synchronizedWithTransaction;
}
 
Example 17
public ExtendedEntityManagerSynchronization(
		EntityManager em, @Nullable PersistenceExceptionTranslator exceptionTranslator) {

	super(new EntityManagerHolder(em), em);
	this.entityManager = em;
	this.exceptionTranslator = exceptionTranslator;
}
 
Example 18
protected void doTestExceptionTranslationWithDialectFound(PersistenceExceptionTranslator pet) throws Exception {
	RuntimeException in1 = new RuntimeException("in1");
	PersistenceException in2 = new PersistenceException();
	assertNull("No translation here", pet.translateExceptionIfPossible(in1));
	DataAccessException dex = pet.translateExceptionIfPossible(in2);
	assertNotNull(dex);
	assertSame(in2, dex.getCause());
}
 
Example 19
@Override
public boolean matches(Method method, Class<?> targetClass) {
	if (TransactionalProxy.class.isAssignableFrom(targetClass) ||
			PlatformTransactionManager.class.isAssignableFrom(targetClass) ||
			PersistenceExceptionTranslator.class.isAssignableFrom(targetClass)) {
		return false;
	}
	TransactionAttributeSource tas = getTransactionAttributeSource();
	return (tas == null || tas.getTransactionAttribute(method, targetClass) != null);
}
 
Example 20
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
 * @param repositoryAnnotationType the annotation type to check for
 */
public PersistenceExceptionTranslationAdvisor(
		PersistenceExceptionTranslator persistenceExceptionTranslator,
		Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example 21
@Override
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	if (AnnotationUtils.findAnnotation(pf.getTargetClass(), Repository.class) != null) {
		DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
		bf.registerBeanDefinition("peti", new RootBeanDefinition(PersistenceExceptionTranslationInterceptor.class));
		bf.registerSingleton("pet", pet);
		pf.addAdvice((PersistenceExceptionTranslationInterceptor) bf.getBean("peti"));
	}
}
 
Example 22
Source Project: spring-data   Source File: ArangoTemplate.java    License: Apache License 2.0 5 votes vote down vote up
public ArangoTemplate(final ArangoDB arango, final String database, final ArangoConverter converter,
	final PersistenceExceptionTranslator exceptionTranslator) {
	super();
	this.arango = arango._setCursorInitializer(new ArangoCursorInitializer(converter));
	this.databaseName = database;
	this.databaseExpression = PARSER.parseExpression(databaseName, ParserContext.TEMPLATE_EXPRESSION);
	this.converter = converter;
	this.exceptionTranslator = exceptionTranslator;
	this.context = new StandardEvaluationContext();
	// set concurrency level to 1 as writes are very rare compared to reads
	collectionCache = new ConcurrentHashMap<>(8, 0.9f, 1);
	databaseCache = new ConcurrentHashMap<>(8, 0.9f, 1);
	version = null;
}
 
Example 23
Source Project: spring-data   Source File: DefaultUserOperation.java    License: Apache License 2.0 5 votes vote down vote up
protected DefaultUserOperation(final ArangoDatabase db, final String username,
	final PersistenceExceptionTranslator exceptionTranslator, final CollectionCallback collectionCallback) {
	this.db = db;
	this.username = username;
	this.exceptionTranslator = exceptionTranslator;
	this.collectionCallback = collectionCallback;
}
 
Example 24
Source Project: spring-data   Source File: DefaultCollectionOperations.java    License: Apache License 2.0 5 votes vote down vote up
protected DefaultCollectionOperations(final ArangoCollection collection,
	final Map<CollectionCacheKey, CollectionCacheValue> collectionCache,
	final PersistenceExceptionTranslator exceptionTranslator) {
	this.collection = collection;
	this.collectionCache = collectionCache;
	this.exceptionTranslator = exceptionTranslator;
}
 
Example 25
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
 * @param repositoryAnnotationType the annotation type to check for
 */
public PersistenceExceptionTranslationAdvisor(
		PersistenceExceptionTranslator persistenceExceptionTranslator,
		Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example 26
private ExtendedEntityManagerInvocationHandler(EntityManager target,
		PersistenceExceptionTranslator exceptionTranslator, Boolean jta,
		boolean containerManaged, boolean synchronizedWithTransaction) {

	this.target = target;
	this.exceptionTranslator = exceptionTranslator;
	this.jta = (jta != null ? jta : isJtaEntityManager());
	this.containerManaged = containerManaged;
	this.synchronizedWithTransaction = synchronizedWithTransaction;
}
 
Example 27
Source Project: dubbox   Source File: SolrTemplate.java    License: Apache License 2.0 5 votes vote down vote up
private void registerPersistenceExceptionTranslator() {
	if (this.applicationContext != null
			&& this.applicationContext.getBeansOfType(PersistenceExceptionTranslator.class).isEmpty()) {
		if (this.applicationContext instanceof ConfigurableApplicationContext) {
			((ConfigurableApplicationContext) this.applicationContext).getBeanFactory()
					.registerSingleton("solrExceptionTranslator", EXCEPTION_TRANSLATOR);
		}
	}
}
 
Example 28
private ExtendedEntityManagerInvocationHandler(EntityManager target,
		PersistenceExceptionTranslator exceptionTranslator, Boolean jta,
		boolean containerManaged, boolean synchronizedWithTransaction) {

	this.target = target;
	this.exceptionTranslator = exceptionTranslator;
	this.jta = (jta != null ? jta : isJtaEntityManager());
	this.containerManaged = containerManaged;
	this.synchronizedWithTransaction = synchronizedWithTransaction;
}
 
Example 29
protected void doTestExceptionTranslationWithDialectFound(PersistenceExceptionTranslator pet) throws Exception {
	RuntimeException in1 = new RuntimeException("in1");
	PersistenceException in2 = new PersistenceException();
	assertNull("No translation here", pet.translateExceptionIfPossible(in1));
	DataAccessException dex = pet.translateExceptionIfPossible(in2);
	assertNotNull(dex);
	assertSame(in2, dex.getCause());
}
 
Example 30
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
 * @param repositoryAnnotationType the annotation type to check for
 */
public PersistenceExceptionTranslationAdvisor(
		PersistenceExceptionTranslator persistenceExceptionTranslator,
		Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}