benjamin-bader / droptools Goto Github PK
View Code? Open in Web Editor NEWAddon bundle for Dropwizard to support jOOQ for database access
Home Page: https://droptools.bendb.com
License: Apache License 2.0
Addon bundle for Dropwizard to support jOOQ for database access
Home Page: https://droptools.bendb.com
License: Apache License 2.0
Is there anyway to get a jedis
object from the pool in a non jersey resource, e.g. in a running eventbus listener?
I noticed that the default DSLContext
that is bound in the JooqBinder
is RequestScoped
, while the instances that injected for @JooqInject
are not.
Is there a reason for this difference? Would there be any adverse effects to removing the .in(RequestScoped.calss)
for the default hk2 binding of DSLContext
?
I am unsure how the connections are being released by:
https://github.com/dropwizard/dropwizard/blob/master/dropwizard-db/src/main/java/io/dropwizard/db/ManagedPooledDataSource.java
I am considering https://github.com/brettwooldridge/HikariCP
sample:
https://gist.github.com/esnellman/2eb3df086baa8769ba60
Naturally I was thinking of modifying Factory where the dispose(instance) would release a connection. The jersey docs are unclear, do you know if dispose is always called even in the event of an exception? Like a try with resource block?
java.lang.NoSuchMethodError: com.google.common.net.HostAndPort.getHostText()Ljava/lang/String;
at com.bendb.dropwizard.redis.JedisFactory.getHost(JedisFactory.java:108)
at com.bendb.dropwizard.redis.JedisFactory.build(JedisFactory.java:168)
at com.bendb.dropwizard.redis.JedisBundle.run(JedisBundle.java:25)
at com.bendb.dropwizard.redis.JedisBundle.run(JedisBundle.java:11)
at io.dropwizard.setup.Bootstrap.run(Bootstrap.java:200)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:42)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
at io.dropwizard.cli.Cli.run(Cli.java:74)
at io.dropwizard.Application.run(Application.java:93)
getHostText()
seems deprecated in guava 22.0public String getHost() {
return endpoint.getHostText();
}
Try to compile the project
/**
* Returns the portion of this {@code HostAndPort} instance that should represent the hostname or
* IPv4/IPv6 literal.
*
* <p>A successful parse does not imply any degree of sanity in this field. For additional
* validation, see the {@link HostSpecifier} class.
*
* @since 20.0 (since 10.0 as {@code getHostText})
*/
public String getHost() {
return host;
}
Thanks!
Hi,
I'm not having any luck getting dropwizard-jooq working in a really simple example that is 99% copied from the https://github.com/benjamin-bader/droptools/tree/master/droptools-example.
My dependencies are:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.8.0-rc1</version>
</dependency>
<dependency>
<groupId>com.bendb.dropwizard</groupId>
<artifactId>dropwizard-jooq</artifactId>
<version>0.7.1-5</version>
</dependency>
And I see this exception at startup, even with no registered resources.
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: com/sun/jersey/spi/inject/InjectableProvider
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.bendb.dropwizard.jooq.JooqBundle.run(JooqBundle.java:28)
at com.bendb.dropwizard.jooq.JooqBundle.run(JooqBundle.java:12)
at io.dropwizard.setup.Bootstrap.run(Bootstrap.java:176)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:41)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76)
at io.dropwizard.cli.Cli.run(Cli.java:70)
at io.dropwizard.Application.run(Application.java:73)
at uk.co.eventgenius.api.ApiApplication.main(ApiApplication.java:23)
... 6 more
Caused by: java.lang.ClassNotFoundException: com.sun.jersey.spi.inject.InjectableProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 25 more
It looks like something's looking for jersey-core but I tried including that to no avail. Have tried cloning droptools-example and while I didn't get it to start up fully (as I didn't setup the db), it loaded farther than i've managed so far separately, and I can't see it using InjectableProvider or having access to it from it's dependencies.
Am I doing something daft?
Thanks,
Steve
When configuring the JooqFactory from the dropwizard config, the enumeration values available for dialect are quite reduced from the ones of original jooq framework, for instance Oracle technologies cannot be selected.
btw great project !
Hi,
Would you mind pushing the latest changes to maven?
Thank you,
joe
I'd like to be able to use multiple JedisBundles (to connect to multiple Redis instances) from the same DropWizard application. I think this is not currently possible/supported. I wonder if it's just because of the name "redis" being hardcoded in the JedisBundle class for healthcheck and metrics? Or is there some deeper reason? If it is the former, should be easy enough to get that value from a protected "getName()" method that can be overridden for multiple bundles.
JedisPool uses an Apache Commons Pool implementation which by default has a maxWaitMillis set to -1 (wait indefinitely). This causes the getPool() connection to hang indefinitely if all the available connections are in use. This is less than ideal from a consumer-point-of-view.
I suggest that we can set the poolConfig maxWaitMillis to match the connection timeout config used for the pool itself. Happy to submit a PR if needed.
Anyone already working toward supporting .9? I might fork and work it.
Exception in thread "main" java.lang.NoSuchMethodError: com.bendb.dropwizard.jooq.JooqBundle.getDataSourceFactory(Lio/dropwizard/Configuration;)Lio/dropwizard/db/DataSourceFactory;
at com.bendb.dropwizard.jooq.JooqBundle.run(JooqBundle.java:33)
at com.bendb.dropwizard.jooq.JooqBundle.run(JooqBundle.java:14)
at io.dropwizard.setup.Bootstrap.run(Bootstrap.java:199)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:39)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:77)
at io.dropwizard.cli.Cli.run(Cli.java:70)
at io.dropwizard.Application.run(Application.java:80)
https://github.com/dropwizard/dropwizard/blob/release/0.9.x/dropwizard-db/src/main/java/io/dropwizard/db/DatabaseConfiguration.java
change to PooledDataSourceFactory from DataSourceFactory
Hi,
Can you upgrade Jedis version to 2.8.0?
Can you also add guages for JedisPool which captures, JedisPool#getNumActive(), JedisPool#getNumIdle(), JedisPool#getNumWaiters()
I am hunting for the ideal way to get a database handle into a task - striking out.
Any suggestions?
Change from and to methods in JodaDateTimeConverter to return null when null is passed in.
For example on application start. I want to use jOOQ in repository classes, not directly in my resources.
We're beginning a migration to Dropwizard 0.8 (realizing this is still an RC). Currently, we're only using the Redis bundle, but will be adding the jOOQ one. I was hoping 0.8 support was being worked on?
In my own attempt, I'm rather confused with HK2/Jersey injection -- at best I could hack something, basically creating an implementation of InjectionResolver. I feel I shouldn't need to do that and instead only need to register ValueFactoryProviders for the pool and a connection and ContextInjectionProvider should be able to resolve it via the ServiceLocator instance it uses -- I have yet to succeed at this.
It'd be awesome if there was a port already started/moved past this. =)
Can the package be updated to use Jedis 2.8.0
Would it be reasonable to cut a release (candidate)?
I'd like to push our migrated (0.8) code to CI, it'd be super helpful if there were droptools jars in maven repos that we could pull down.
I want to select redis db index for my App, like
jedis.select(2)
any help will be highly appreciated,
Hi,
when do you plan on releasing dropwizard-jooq
for dropwizard 2.x.x ?
I'm trying to update to dw 2.0.2 and it seems your very useful lib is the last missing piece !
Related question: do you push your snapshot builds on a public maven somewhere ? (And would you be willing to do so ?) That would help me test in the meantime.
Thanks a lot for your work !
I have included the following dependency on gradle dependencies but Its not resolving the package.
Unable to import jedis and bedb not found.
compile group: 'com.bendb.dropwizard', name: 'dropwizard-redis', version: '1.0.0-0'
In using droptools, do the injected DSLContext's into various resource classes need to be micromanaged (closed each time) or is that taken care of elsewhere?
Fluent creation of a DSLContext object
Interact with JDBC Connections
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public class TestResource {
public TestResource () {}
@GET
@Path("/test/{testId}")
public TestDTO getTest(@JooqInject DSLContext database, @Auth DashboardUser dashboardUser, @PathParam("testId") IntParam testId) {
// Do things with database connection
// Return results as JSON
// Close context connection?
}
Cheers
Why is the Redis timeout not configurable? The default timeout of 2000ms may not always work.
Hi again !
Importing dropwizard-jooq:2.0.2-0
along with dropwizard-core:2.0.2
creates duplicated classes/ressources issues at compilation. Some equal some different.
Classes/ressources are in jakarta.xml.bind-api:2.3.2
/ jakarta.activation-api:1.2.1
and jaxb-api:2.3.1
/ javax.activation-api:1.2.0
.
dropwizard-core:2.0.2
, dropwizard-testing:2.0.2
and dropwizard-migrations:2.0.2
all transitively depends on jakarta.xml.bind-api:2.3.2
> jakarta.activation-api:1.2.1
.
dropwizard-jooq:2.0.2-0
depends on jooq:3.13.1
> jaxb-api:2.3.1
> javax.activation-api:1.2.0
.
I tried to work around that by excluding jaxb-api from dropwizard-jooq in maven, but I don't know if that is viable as I encountered another (and unrelated) issue that will need an external fix.
I don't know what would be a good fix for you on this issue, because it seems that dropwizard added that runtime dependency in dw 2.0.1 (it was not in 2.0.0) straight at jakarta.xml.bind-api:2.3.2
. And jooq 3.13.1 is the latest available for now. I'm afraid that, considering these are different artifacts packaging same classes, even aligning versions would still bring duplicates (though they should be equals)...
What do you think ?
Hello!
I was wondering if you plan to update this library to work with the breaking changes in Dropwizard 3 and 4, a PooledDataSourceFactory
is now needed over a DataSourceFactory
in the new bundle.
Dan
Hi Ben,
Just wondering when you'll be able to do a release or upload a snapshot to Sonatype Nexus. We here at Lasso are eager to utilize the recent changes from a public domain.
Thanks!
I'm trying to unit test a Resource that expects a Jedis instance (via @context), via GrizzlyTestContainer, but I fail to succeed.
@GET
@Path("/foo")
@ManagedAsync
public void checkFoo(@Suspended AsyncResponse response, @Context final Jedis jedis) {
...
}
@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
.addResource(new MyResource())
.setTestContainerFactory(new GrizzlyWebTestContainerFactory())
.addProvider(...)
.build();
How can I inject e.g. a mock Jedis instance? Should it go via addProvider()?
More generally, the output from mvn site:site site:stage
contains data for dropwizard-jooq
only; neither the parent module nor dropwizard-redis
are present at all.
This used to work, at least momentarily. Maybe we should just set up Sphinx integration like Dropwizard does, Maven is a pile of bull-honky for this stuff.
It seems like something related to Maven breaks every time I look at this project. The Java world has largely moved on to Gradle, and we should move on as well. Lord knows the release process feels more reliable using gradle-mvn-push
.
The Need
We've found so far that our resource methods are our transactional boundary 99% of the time. A convenient @UnitOfWork annotation for our resource methods which seamlessly wrap DSLContexts used by our DAO classes inside one transaction is really helpful. We'd like our DAO code to not have to worry about whether it's within a transaction or not.
General Approach
So instead of a Hibernate SessionFactory returning the same Session, we have a DslContextFactory (see snippets) returning the same DSLContext in case there is a transaction such that our DAO classes don't care if they're operating within or without a transaction. So I created a factory, which if a transaction has been started will return the same DSLContext to anything that asks for one. This is tracked via a ThreadLocal -- scary I know!
Questions
Approach: Details
The way I got this work in my scenario was creating a RequestDispatcher implementation which would look for a UnitOfWork annotation, and if present would start a transaction. Inside of the callback for the transaction call it would set a ThreadLocal in a DslContextFactory (Reuses the ThreadLocal DSLContext or produces a new one) and call the regular dispatcher (to continue the chain).
Relevant Code Snippets
I wrote this with Java 8, but I think the point should be clear.
UnitOfWorkRequestDispatcher
public final class UnitOfWorkRequestDispatcher implements RequestDispatcher {
private final RequestDispatcher dispatcher;
private final DslContextFactory contextFactory;
public UnitOfWorkRequestDispatcher(RequestDispatcher dispatcher, DslContextFactory contextFactory) {
this.dispatcher = dispatcher;
this.contextFactory = contextFactory;
}
@Override
public void dispatch(final Object resource, final HttpContext context) {
try {
contextFactory.getContext().transaction(new TransactionalRunnable() {
@Override
public void run(Configuration configuration) throws Exception {
contextFactory.cacheThreadConfiguration(configuration);
dispatcher.dispatch(resource, context);
}
});
} finally {
contextFactory.invalidateThreadConfigurationCache();
}
}
}
DslContextFactory
public final class DslContextFactory {
private static final ThreadLocal<Configuration> cachedConfiguration =
new ThreadLocal<Configuration>() {
@Override
protected Configuration initialValue() {
return null;
}
};
private final Configuration configuration;
public DslContextFactory(ManagedDataSource dataSource, SQLDialect dialect, ExecuteListener... listeners) {
final ExecuteListenerProvider[] providers = new ExecuteListenerProvider[listeners.length];
for (int i = 0; i < listeners.length; i++) {
providers[i] = new DefaultExecuteListenerProvider(listeners[i]);
}
this.configuration = new DefaultConfiguration().set(dataSource).set(dialect).set(providers);
}
public DSLContext getContext() {
if (cachedConfiguration.get() != null) {
return DSL.using(cachedConfiguration.get());
}
return DSL.using(configuration);
}
public void cacheThreadConfiguration(Configuration configuration) {
checkNotNull(configuration);
cachedConfiguration.set(configuration);
}
public void invalidateThreadConfigurationCache() {
cachedConfiguration.remove();
}
}
The release on github occurred 25 days ago, but it hasn't been published yet for consumption by maven. https://oss.sonatype.org/#nexus-search;quick~dropwizard-redis
Is there a plan to publish the artifact there?
Dropwizard 1.2.0 has upgraded to guava 23.1 (specifically com.google.guava:guava:23.1-jre
) which has officially removed the getHostString()
method on the HostAndPort
object in the com.google.common.net
package.
Deprecation notes: https://google.github.io/guava/releases/20.0/api/docs/deprecated-list.html
I think the "fix" to make your awesome JedisFactory work in a dropwizard 1.2.0 environment is as easy as replacing the call on line 108 from endpoint.getHostString();
to endpoint.getHost()
I'll submit a PR shortly for that and it'd be awesome if you could release a new dropwizard-redis for dropwizard 1.2.0 compatibility.
Hi
In Jooq there is already LoggerListener
, why do we need LoggingExecutionListener
then?
Is that some remnant of an old time when there wasn't any SQL logger?
The jooq LoggerListener
seems to handle lots of corner cases in any case.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.