Skip to content

Commit

Permalink
Merge pull request #7089 from jaikiran/qk-7079
Browse files Browse the repository at this point in the history
Force loading of JDBC drivers in runtime classloader
  • Loading branch information
stuartwdouglas authored Feb 10, 2020
2 parents 5389fac + 33de842 commit e59e07c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.sql.Driver;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;

import javax.annotation.PreDestroy;
import javax.inject.Inject;
Expand Down Expand Up @@ -73,6 +75,8 @@ public AgroalDataSource createDataSource(String dataSourceName,
log.warn("Datasource " + dataSourceName + " not started: driver and/or url are not defined.");
return null;
}
// we first make sure that all available JDBC drivers are loaded in the current TCCL
loadDriversInTCCL();

DataSourceRuntimeConfig dataSourceRuntimeConfig = dataSourceRuntimeConfigOptional.get();

Expand Down Expand Up @@ -244,6 +248,24 @@ private void checkRuntimeConfig() {
}
}

/**
* Uses the {@link ServiceLoader#load(Class) ServiceLoader to load the JDBC drivers} in context
* of the current {@link Thread#getContextClassLoader() TCCL}
*/
private static void loadDriversInTCCL() {
// load JDBC drivers in the current TCCL
final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
final Iterator<Driver> iterator = drivers.iterator();
while (iterator.hasNext()) {
try {
// load the driver
iterator.next();
} catch (Throwable t) {
// ignore
}
}
}

@PreDestroy
public void stop() {
for (AgroalDataSource dataSource : dataSources) {
Expand Down
20 changes: 20 additions & 0 deletions extensions/smallrye-opentracing/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.quarkus.smallrye.opentracing.deployment;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.QueryHint;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "known_fruits")
@NamedQuery(name = "Fruits.findAll", query = "SELECT f FROM Fruit f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true"))
@Cacheable
public class Fruit {

@Id
@SequenceGenerator(name = "fruitsSequence", sequenceName = "known_fruits_id_seq", allocationSize = 1, initialValue = 10)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "fruitsSequence")
private Integer id;

@Column(length = 40, unique = true)
private String name;

public Fruit() {
}

public Fruit(String name) {
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand All @@ -30,14 +30,17 @@ public class TracingTest {
.addClass(TestResource.class)
.addClass(Service.class)
.addClass(RestService.class)
.addClass(Fruit.class)
.addAsResource("application.properties")
.addAsResource("import.sql")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

static MockTracer mockTracer = new MockTracer();
static {
GlobalTracer.register(mockTracer);
}

@AfterEach
@BeforeEach
public void after() {
mockTracer.reset();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
quarkus.datasource.driver=io.opentracing.contrib.jdbc.TracingDriver
quarkus.datasource.url=jdbc:tracing:h2:mem:test
quarkus.datasource.max-size=8
quarkus.datasource.min-size=2
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.log.sql=true
quarkus.hibernate-orm.sql-load-script=import.sql
quarkus.hibernate-orm.dialect=org.hibernate.dialect.H2Dialect

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INSERT INTO known_fruits(id, name) VALUES (1, 'Cherry');
INSERT INTO known_fruits(id, name) VALUES (2, 'Apple');
INSERT INTO known_fruits(id, name) VALUES (3, 'Banana');

0 comments on commit e59e07c

Please sign in to comment.