Search in sources :

Example 1 with SqlgPlugin

use of org.umlg.sqlg.SqlgPlugin in project sqlg by pietermartin.

the class SqlgAbstractGraphProvider method clear.

@Override
public void clear(final Graph g, final Configuration configuration) throws Exception {
    logger.debug("clearing datasource " + configuration.getString("jdbc.url"));
    SqlgDataSource sqlgDataSource = null;
    if (null != g) {
        if (g.features().graph().supportsTransactions() && g.tx().isOpen()) {
            g.tx().rollback();
        }
        g.close();
    }
    SqlgPlugin plugin = getSqlgPlugin();
    SqlDialect sqlDialect = plugin.instantiateDialect();
    try {
        sqlgDataSource = SqlgGraph.createDataSourceFactory(configuration).setup(plugin.getDriverFor(configuration.getString("jdbc.url")), configuration);
        try (Connection conn = sqlgDataSource.getDatasource().getConnection()) {
            SqlgUtil.dropDb(sqlDialect, conn);
        }
    } catch (PropertyVetoException e) {
        throw new RuntimeException(e);
    } finally {
        if (sqlgDataSource != null) {
            sqlgDataSource.close();
        }
    }
}
Also used : SqlgDataSource(org.umlg.sqlg.structure.SqlgDataSourceFactory.SqlgDataSource) PropertyVetoException(java.beans.PropertyVetoException) Connection(java.sql.Connection) SqlDialect(org.umlg.sqlg.sql.dialect.SqlDialect) SqlgPlugin(org.umlg.sqlg.SqlgPlugin)

Example 2 with SqlgPlugin

use of org.umlg.sqlg.SqlgPlugin in project sqlg by pietermartin.

the class TestJNDIInitialization method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
    configuration = new PropertiesConfiguration(sqlProperties);
    if (!configuration.containsKey("jdbc.url")) {
        throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));
    }
    String url = configuration.getString("jdbc.url");
    // obtain the connection that we will later supply from JNDI
    SqlgPlugin p = findSqlgPlugin(url);
    Assert.assertNotNull(p);
    ds = new C3p0DataSourceFactory().setup(p.getDriverFor(url), configuration).getDatasource();
    // change the connection url to be a JNDI one
    configuration.setProperty("jdbc.url", "jndi:testConnection");
    // set up the initial context
    NamingManager.setInitialContextFactoryBuilder(environment -> {
        InitialContextFactory mockFactory = mock(InitialContextFactory.class);
        Context mockContext = mock(Context.class);
        when(mockFactory.getInitialContext(any())).thenReturn(mockContext);
        when(mockContext.lookup("testConnection")).thenReturn(ds);
        return mockFactory;
    });
}
Also used : Context(javax.naming.Context) C3p0DataSourceFactory(org.umlg.sqlg.structure.ds.C3p0DataSourceFactory) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) SqlgPlugin(org.umlg.sqlg.SqlgPlugin) InitialContextFactory(javax.naming.spi.InitialContextFactory) URL(java.net.URL) BeforeClass(org.junit.BeforeClass)

Aggregations

SqlgPlugin (org.umlg.sqlg.SqlgPlugin)2 PropertyVetoException (java.beans.PropertyVetoException)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 Context (javax.naming.Context)1 InitialContextFactory (javax.naming.spi.InitialContextFactory)1 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)1 BeforeClass (org.junit.BeforeClass)1 SqlDialect (org.umlg.sqlg.sql.dialect.SqlDialect)1 SqlgDataSource (org.umlg.sqlg.structure.SqlgDataSourceFactory.SqlgDataSource)1 C3p0DataSourceFactory (org.umlg.sqlg.structure.ds.C3p0DataSourceFactory)1