Search in sources :

Example 1 with ConnectionProvider

use of org.jooq.ConnectionProvider in project spring-boot by spring-projects.

the class JooqAutoConfigurationTests method jooqWithDefaultConnectionProvider.

@Test
void jooqWithDefaultConnectionProvider() {
    this.contextRunner.withUserConfiguration(JooqDataSourceConfiguration.class).run((context) -> {
        DSLContext dsl = context.getBean(DSLContext.class);
        ConnectionProvider connectionProvider = dsl.configuration().connectionProvider();
        assertThat(connectionProvider).isInstanceOf(DataSourceConnectionProvider.class);
        DataSource connectionProviderDataSource = ((DataSourceConnectionProvider) connectionProvider).dataSource();
        assertThat(connectionProviderDataSource).isInstanceOf(TransactionAwareDataSourceProxy.class);
    });
}
Also used : DataSourceConnectionProvider(org.jooq.impl.DataSourceConnectionProvider) DSLContext(org.jooq.DSLContext) ConnectionProvider(org.jooq.ConnectionProvider) DataSourceConnectionProvider(org.jooq.impl.DataSourceConnectionProvider) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectionProvider

use of org.jooq.ConnectionProvider in project jOOQ by jOOQ.

the class DefaultDSLContext method close.

// -------------------------------------------------------------------------
// XXX AutoCloseable
// -------------------------------------------------------------------------
@Override
public void close() {
    ConnectionProvider cp = configuration().connectionProvider();
    if (cp instanceof DefaultConnectionProvider) {
        DefaultConnectionProvider dcp = (DefaultConnectionProvider) cp;
        if (dcp.finalize) {
            JDBCUtils.safeClose(dcp.connection);
            dcp.connection = null;
        }
    }
}
Also used : ConnectionProvider(org.jooq.ConnectionProvider)

Example 3 with ConnectionProvider

use of org.jooq.ConnectionProvider in project vertx-zero by silentbalanceyh.

the class JooqInfix method initInternal.

private static void initInternal(final Vertx vertx, final String name) {
    vertxRef = vertx;
    Fn.pool(CONFIGS, name, () -> Infix.init(Plugins.Infix.JOOQ, (config) -> {
        // Initialized client
        final Configuration configuration = new DefaultConfiguration();
        configuration.set(SQLDialect.MYSQL_8_0);
        final ConnectionProvider provider = new DefaultConnectionProvider(HikariCpPool.getConnection(config.getJsonObject("provider")));
        // Initialized default configuration
        configuration.set(provider);
        return configuration;
    }, JooqInfix.class));
}
Also used : Fn(io.vertx.up.func.Fn) HikariCpPool(io.vertx.tp.hikari.HikariCpPool) ConnectionProvider(org.jooq.ConnectionProvider) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Infix(io.vertx.up.plugin.Infix) Vertx(io.vertx.core.Vertx) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) Plugin(io.vertx.up.annotations.Plugin) Instance(io.vertx.up.tool.mirror.Instance) ConcurrentMap(java.util.concurrent.ConcurrentMap) Configuration(org.jooq.Configuration) DSLContext(org.jooq.DSLContext) Plugins(io.vertx.up.eon.Plugins) SQLDialect(org.jooq.SQLDialect) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Configuration(org.jooq.Configuration) DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) ConnectionProvider(org.jooq.ConnectionProvider) DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider)

Example 4 with ConnectionProvider

use of org.jooq.ConnectionProvider in project jOOQ by jOOQ.

the class SparkCRUD method main.

public static void main(String[] args) throws Exception {
    final BasicDataSource ds = new BasicDataSource();
    final Properties properties = new Properties();
    properties.load(SparkCRUD.class.getResourceAsStream("/config.properties"));
    ds.setDriverClassName(properties.getProperty("db.driver"));
    ds.setUrl(properties.getProperty("db.url"));
    ds.setUsername(properties.getProperty("db.username"));
    ds.setPassword(properties.getProperty("db.password"));
    final ConnectionProvider cp = new DataSourceConnectionProvider(ds);
    final Configuration configuration = new DefaultConfiguration().set(cp).set(SQLDialect.H2).set(new ThreadLocalTransactionProvider(cp, true));
    final DSLContext ctx = DSL.using(configuration);
    final JSONFormat format = new JSONFormat().format(true).header(false).recordFormat(RecordFormat.OBJECT);
    // Creates a new book resource, will return the ID to the created resource
    // author and title are sent as query parameters e.g. /books?author=Foo&title=Bar
    post("/books", (request, response) -> {
        return ctx.transactionResult(() -> {
            AuthorRecord author = upsertAuthor(ctx, request);
            BookRecord book = ctx.newRecord(BOOK);
            book.setAuthorId(author.getId());
            book.setTitle(request.queryParams("title"));
            book.store();
            // 201 Created
            response.status(201);
            return book.formatJSON(format);
        });
    });
    // Gets the book resource for the provided id
    get("/books/:id", (request, response) -> {
        Record2<String, String> book = ctx.select(BOOK.TITLE, AUTHOR.NAME).from(BOOK).join(AUTHOR).on(BOOK.AUTHOR_ID.eq(AUTHOR.ID)).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).fetchOne();
        if (book != null) {
            return book.formatJSON(format);
        } else {
            // 404 Not found
            response.status(404);
            return "{\"error\":\"Book not found\"}";
        }
    });
    // Updates the book resource for the provided id with new information
    // author and title are sent as query parameters e.g. /books/<id>?author=Foo&title=Bar
    put("/books/:id", (request, response) -> {
        return ctx.transactionResult(() -> {
            BookRecord book = ctx.selectFrom(BOOK).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).fetchOne();
            if (book != null) {
                AuthorRecord author = upsertAuthor(ctx, request);
                String newAuthor = request.queryParams("author");
                String newTitle = request.queryParams("title");
                if (newAuthor != null)
                    book.setAuthorId(author.getId());
                if (newTitle != null)
                    book.setTitle(newTitle);
                book.update();
                return book.formatJSON(format);
            } else {
                // 404 Not found
                response.status(404);
                return "{\"error\":\"Book not found\"}";
            }
        });
    });
    // Deletes the book resource for the provided id
    delete("/books/:id", (request, response) -> {
        return ctx.transactionResult(() -> {
            BookRecord book = ctx.deleteFrom(BOOK).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).returning().fetchOne();
            if (book != null) {
                return book.formatJSON(format);
            } else {
                // 404 Not found
                response.status(404);
                return "{\"error\":\"Book not found\"}";
            }
        });
    });
    // Gets all available book resources
    get("/books", (request, response) -> {
        return ctx.select(BOOK.ID, BOOK.TITLE).from(BOOK).fetch().formatJSON(format);
    });
}
Also used : DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Configuration(org.jooq.Configuration) DataSourceConnectionProvider(org.jooq.impl.DataSourceConnectionProvider) ThreadLocalTransactionProvider(org.jooq.impl.ThreadLocalTransactionProvider) DSLContext(org.jooq.DSLContext) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Properties(java.util.Properties) JSONFormat(org.jooq.JSONFormat) ConnectionProvider(org.jooq.ConnectionProvider) DataSourceConnectionProvider(org.jooq.impl.DataSourceConnectionProvider) AuthorRecord(org.jooq.example.db.h2.tables.records.AuthorRecord) BookRecord(org.jooq.example.db.h2.tables.records.BookRecord) BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Example 5 with ConnectionProvider

use of org.jooq.ConnectionProvider in project jOOQ by jOOQ.

the class DefaultCloseableDSLContext method close.

// -------------------------------------------------------------------------
// XXX AutoCloseable
// -------------------------------------------------------------------------
@Override
public void close() {
    ConnectionProvider cp = configuration().connectionProvider();
    ConnectionFactory cf = configuration().connectionFactory();
    if (cp instanceof DefaultCloseableConnectionProvider) {
        DefaultCloseableConnectionProvider dcp = (DefaultCloseableConnectionProvider) cp;
        JDBCUtils.safeClose(dcp.connection);
        dcp.connection = null;
    }
    if (cf instanceof DefaultConnectionFactory) {
        DefaultConnectionFactory dcf = (DefaultConnectionFactory) cf;
        if (dcf.finalize) {
            R2DBC.block(dcf.connection.close());
            dcf.connection = null;
        }
    }
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ConnectionProvider(org.jooq.ConnectionProvider)

Aggregations

ConnectionProvider (org.jooq.ConnectionProvider)6 DSLContext (org.jooq.DSLContext)3 Configuration (org.jooq.Configuration)2 DataSourceConnectionProvider (org.jooq.impl.DataSourceConnectionProvider)2 DefaultConfiguration (org.jooq.impl.DefaultConfiguration)2 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)1 Vertx (io.vertx.core.Vertx)1 HikariCpPool (io.vertx.tp.hikari.HikariCpPool)1 Plugin (io.vertx.up.annotations.Plugin)1 Plugins (io.vertx.up.eon.Plugins)1 Fn (io.vertx.up.func.Fn)1 Infix (io.vertx.up.plugin.Infix)1 Instance (io.vertx.up.tool.mirror.Instance)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 DataSource (javax.sql.DataSource)1 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)1 JSONFormat (org.jooq.JSONFormat)1 SQLDialect (org.jooq.SQLDialect)1