Search in sources :

Example 21 with CommonDataSource

use of javax.sql.CommonDataSource in project tomee by apache.

the class FlushableDataSourceHandler method updateDataSource.

public void updateDataSource(final CommonDataSource ds) {
    // order is important, check DataSourceFactory
    CommonDataSource current = ds;
    while (Proxy.isProxyClass(current.getClass())) {
        final InvocationHandler handler = Proxy.getInvocationHandler(current);
        if (FlushableDataSourceHandler.class.isInstance(handler) || ResettableDataSourceHandler.class.isInstance(handler)) {
            current = DelegatableHandler.class.cast(handler).getDelegate();
        } else {
            break;
        }
    }
    delegate.set(current);
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler) CommonDataSource(javax.sql.CommonDataSource)

Example 22 with CommonDataSource

use of javax.sql.CommonDataSource in project tomee by apache.

the class FlushableDataSourceHandler method createANewDelegate.

private void createANewDelegate() {
    final CommonDataSource old = delegate.get();
    try {
        final ObjectRecipe recipe = new ObjectRecipe(DataSourceFactory.class.getName(), "create", FACTORY_ARGS);
        recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
        recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
        recipe.allow(Option.NAMED_PARAMETERS);
        recipe.allow(Option.PRIVATE_PROPERTIES);
        recipe.setAllProperties(config.properties);
        recipe.setProperty("resettableHandler", resettableHandler);
        recipe.setProperty("flushableHandler", this);
        updateDataSource(CommonDataSource.class.cast(recipe.create()));
    } catch (final Exception e) {
        LOGGER.error("Can't recreate the datasource, keeping old one", e);
        return;
    }
    if (DataSourceFactory.knows(old)) {
        try {
            DataSourceFactory.destroy(old);
        } catch (final Throwable t) {
        // Ignore
        }
    }
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) CommonDataSource(javax.sql.CommonDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 23 with CommonDataSource

use of javax.sql.CommonDataSource in project tomee by apache.

the class FlushableDataSourceHandler method invoke.

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    final CommonDataSource actualDelegate = delegate.get();
    if (Object.class == method.getDeclaringClass()) {
        if ("hashCode".equals(method.getName())) {
            return hashCode();
        }
        if ("toString".equals(method.getName())) {
            return "Flushable[" + actualDelegate.toString() + "]";
        }
    }
    if (Flushable.class == method.getDeclaringClass()) {
        final Lock l = lock.writeLock();
        l.lock();
        try {
            createANewDelegate();
            if (Flushable.class.isInstance(actualDelegate)) {
                // these sanity could be enhanced
                if (!Proxy.isProxyClass(actualDelegate.getClass()) || // reset implies flush so we need to check both
                (!FlushableDataSourceHandler.class.isInstance(Proxy.getInvocationHandler(actualDelegate)) && !ResettableDataSourceHandler.class.isInstance(Proxy.getInvocationHandler(actualDelegate)))) {
                    Flushable.class.cast(actualDelegate).flush();
                }
            }
        } finally {
            l.unlock();
        }
        return null;
    }
    final Lock l = lock.readLock();
    l.lock();
    try {
        return method.invoke(getDelegate(), args);
    } catch (final InvocationTargetException ite) {
        throw ite.getCause();
    } finally {
        l.unlock();
    }
}
Also used : Flushable(java.io.Flushable) CommonDataSource(javax.sql.CommonDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Aggregations

CommonDataSource (javax.sql.CommonDataSource)23 EntityModel (io.requery.meta.EntityModel)12 Configuration (io.requery.sql.Configuration)12 ConfigurationBuilder (io.requery.sql.ConfigurationBuilder)12 SchemaModifier (io.requery.sql.SchemaModifier)12 Before (org.junit.Before)12 EntityCacheBuilder (io.requery.cache.EntityCacheBuilder)6 Platform (io.requery.sql.Platform)5 HSQL (io.requery.sql.platform.HSQL)5 CacheManager (javax.cache.CacheManager)5 CachingProvider (javax.cache.spi.CachingProvider)5 EntityDataStore (io.requery.sql.EntityDataStore)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 DataSource (javax.sql.DataSource)4 XADataSource (javax.sql.XADataSource)4 EmptyEntityCache (io.requery.cache.EmptyEntityCache)3 TableCreationMode (io.requery.sql.TableCreationMode)3 SQLite (io.requery.sql.platform.SQLite)3 SQLException (java.sql.SQLException)3 Flushable (java.io.Flushable)2