Skip to content

Commit

Permalink
Separate context into before and after. Change values that may or may…
Browse files Browse the repository at this point in the history
… not be present to Optional.
  • Loading branch information
chiroito committed Apr 16, 2024
1 parent f6fae83 commit bd0864d
Show file tree
Hide file tree
Showing 124 changed files with 4,292 additions and 2,777 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.postgresql.ds.PGPoolingDataSource;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.PreparedStatement;
Expand Down Expand Up @@ -35,9 +36,7 @@ public static void main(String[] args) throws Exception {

JfrDataSource jfrDs = new JfrDataSource(postgreDs);

try (Connection con = jfrDs.getConnection();
PreparedStatement stmt = con.prepareStatement("SELECT datname FROM pg_database");
ResultSet rs = stmt.executeQuery()) {
try (Connection con = jfrDs.getConnection(); PreparedStatement stmt = con.prepareStatement("SELECT datname FROM pg_database"); ResultSet rs = stmt.executeQuery()) {

rs.next();
System.out.println(rs.getString("datname"));
Expand All @@ -46,6 +45,11 @@ public static void main(String[] args) throws Exception {
}

r.stop();
r.dump(Files.createFile(Paths.get("DemoApp.jfr")));

Path dumpPath = Paths.get("DemoApp.jfr");
Files.deleteIfExists(dumpPath);
r.dump(Files.createFile(dumpPath));

r.close();
}
}
15 changes: 15 additions & 0 deletions jfr4jdbc-driver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>0</forkCount>
<argLine>
--add-opens dev.jfr4jdbc/dev.jfr4jdbc.*=ALL-UNNAMED
--add-exports dev.jfr4jdbc/dev.jfr4jdbc.*=ALL-UNNAMED
--illegal-access=permit
</argLine>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

@MetadataDefinition
@Relational
@Name("dev.jfr4jdbc.ConnectionId")
@Name("jdbc.ConnectionId")
@Label("Connection ID")
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ConnectionId {
public @interface ConnectionIdRelational {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

@MetadataDefinition
@Relational
@Name("dev.jfr4jdbc.DataSourceLabel")
@Name("jdbc.DataSourceLabel")
@Label("DataSource Label")
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DataSourceLabel {
public @interface DataSourceLabelRelational {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.jfr4jdbc;

import dev.jfr4jdbc.interceptor.ConnectionInfo;
import dev.jfr4jdbc.interceptor.InterceptorFactory;
import dev.jfr4jdbc.interceptor.InterceptorManager;
import dev.jfr4jdbc.interceptor.ConnectionInfo;
import dev.jfr4jdbc.interceptor.OperationInfo;

import java.io.InputStream;
Expand All @@ -19,7 +19,7 @@ public class JfrCallableStatement extends JfrPreparedStatement implements Callab
private CallableStatement jdbcStatement;

public JfrCallableStatement(CallableStatement c, String sql) {
this(c, sql, InterceptorManager.getDefaultInterceptorFactory());
this(c, sql, InterceptorManager.instance.getDefaultInterceptorFactory());
}

public JfrCallableStatement(CallableStatement c, String sql, InterceptorFactory factory) {
Expand Down
110 changes: 70 additions & 40 deletions jfr4jdbc-driver/src/main/java/dev/jfr4jdbc/JfrConnection.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,65 @@
package dev.jfr4jdbc;

import dev.jfr4jdbc.interceptor.*;
import dev.jfr4jdbc.internal.Label;
import dev.jfr4jdbc.internal.ResourceMonitor;
import dev.jfr4jdbc.internal.ResourceMonitorManager;
import dev.jfr4jdbc.internal.*;

import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

public class JfrConnection implements Connection {

private static final String LABEL_IS_NOT_SPECIFIED = "userManagedConnection";
private static final ConnectionCounter userManagedConnectionCounter = new ConnectionCounter();
private static final WrappedConnectionCounter userManagedWrappedConnectionCounter = new WrappedConnectionCounter();

protected final Connection connection;

private final ConnectionInfo connectionInfo;
private final Connection connection;
private final Class<? extends Connection> connectionClass;

private final InterceptorFactory interceptorFactory;

private final ResourceMonitor resourceMonitor;

private final AtomicInteger operationCounter = new AtomicInteger(1);
private final ConnectionInfo connectionInfo;
private final OperationCounter operationCounter = new OperationCounter();


public JfrConnection(Connection con) {
this(con, InterceptorManager.getDefaultInterceptorFactory(), LABEL_IS_NOT_SPECIFIED, ConnectionInfo.NO_INFO);
this(con, null, null);
}

public JfrConnection(Connection con, String label) {
this(con, InterceptorManager.getDefaultInterceptorFactory(), label, ConnectionInfo.NO_INFO);
this(con, null, label);
}

public JfrConnection(Connection con, InterceptorFactory factory) {
this(con, factory, LABEL_IS_NOT_SPECIFIED, ConnectionInfo.NO_INFO);
this(con, factory, null);
}

public JfrConnection(Connection con, InterceptorFactory factory, String label) {
this(con, factory, label, ConnectionInfo.NO_INFO);
}

private JfrConnection(Connection con, InterceptorFactory factory, String label, ConnectionInfo connectionInfo) {
private JfrConnection(Connection con, InterceptorFactory factory, String label) {
super();
this.connection = con;
this.interceptorFactory = factory;
this.resourceMonitor = ResourceMonitorManager.getInstance().getOrCreateResourceMonitor(new Label(label));
this.connectionInfo = connectionInfo;
if (factory == null) {
this.interceptorFactory = InterceptorManager.instance.getDefaultInterceptorFactory();
} else {
this.interceptorFactory = factory;
}

LabelCreator labelCreator = null;
if (label == null) {
labelCreator = new UserManagedConnectionLabelCreator();
} else {
labelCreator = new InputLabelFactory(label);
}
DataSourceLabel dataSourceLabel = labelCreator.create();

ConnectionId connectionId = userManagedConnectionCounter.getNewId();
WrappedConnectionId wrappedConnectionId = userManagedWrappedConnectionCounter.getWrappedConnectionId(con);
this.connectionInfo = new ConnectionInfo(dataSourceLabel, connectionId, wrappedConnectionId);

ResourceUnwrapper<Connection> resourceUnwrapper = new ResourceUnwrapper<>();
this.connectionClass = resourceUnwrapper.getWrappedResouce(Connection.class, con).getClass();

this.resourceMonitor = ResourceMonitorManager.getInstance().getOrCreateResourceMonitor(dataSourceLabel);
this.resourceMonitor.useResource();
}

Expand All @@ -57,8 +68,10 @@ public JfrConnection(Connection con, InterceptorFactory factory, ResourceMonitor
super();
this.connection = con;
this.interceptorFactory = factory;
this.resourceMonitor = resourceMonitor;
this.connectionInfo = connectionInfo;
ResourceUnwrapper<Connection> resourceUnwrapper = new ResourceUnwrapper<>();
this.connectionClass = resourceUnwrapper.getWrappedResouce(Connection.class, con).getClass();
this.resourceMonitor = resourceMonitor;

this.resourceMonitor.useResource();
}
Expand All @@ -69,15 +82,15 @@ public ResourceMetrics getResourceMetrics() {
}

private JfrStatement createStatement(Statement s) {
return new JfrStatement(s, interceptorFactory, connectionInfo, new OperationInfo(operationCounter.getAndIncrement()));
return new JfrStatement(s, Statement.class, interceptorFactory, connectionInfo, new OperationInfo(operationCounter.getNewId()));
}

private JfrPreparedStatement createPreparedStatement(PreparedStatement p, String sql) {
return new JfrPreparedStatement(p, sql, interceptorFactory, connectionInfo, new OperationInfo(operationCounter.getAndIncrement()));
return new JfrPreparedStatement(p, sql, interceptorFactory, connectionInfo, new OperationInfo(operationCounter.getNewId()));
}

private JfrCallableStatement createCallableStatement(CallableStatement c, String sql) {
return new JfrCallableStatement(c, sql, interceptorFactory, connectionInfo, new OperationInfo(operationCounter.getAndIncrement()));
return new JfrCallableStatement(c, sql, interceptorFactory, connectionInfo, new OperationInfo(operationCounter.getNewId()));
}

public ConnectionInfo getConnectionInfo() {
Expand All @@ -87,47 +100,64 @@ public ConnectionInfo getConnectionInfo() {
@Override
public void commit() throws SQLException {

Interceptor<CommitContext> interceptor = interceptorFactory.createCommitInterceptor();
CommitContext context = new CommitContext(this.connection, connectionInfo, new OperationInfo(operationCounter.getAndIncrement()));
Interceptor<CommitBeforeInvokeContext, CommitAfterInvokeContext> interceptor = interceptorFactory.createCommitInterceptor();
CommitBeforeInvokeContext beforeContext = new CommitBeforeInvokeContext(this.connection, this.connectionClass, connectionInfo, new OperationInfo(operationCounter.getNewId()));
CommitAfterInvokeContext afterContext = null;
try {
interceptor.preInvoke(context);
interceptor.beforeInvoke(beforeContext);

this.connection.commit();

afterContext = new CommitAfterInvokeContext();

} catch (SQLException | RuntimeException e) {
context.setException(e);
afterContext = new CommitAfterInvokeContext(e);
throw e;
} finally {
interceptor.postInvoke(context);
interceptor.afterInvoke(beforeContext, afterContext);
}
}

@Override
public void rollback() throws SQLException {
Interceptor<RollbackContext> interceptor = interceptorFactory.createRollbackInterceptor();
RollbackContext context = new RollbackContext(this.connection, connectionInfo, new OperationInfo(operationCounter.getAndIncrement()));

Interceptor<RollbackBeforeInvokeContext, RollbackAfterInvokeContext> interceptor = interceptorFactory.createRollbackInterceptor();
RollbackBeforeInvokeContext beforeContext = new RollbackBeforeInvokeContext(this.connection, this.connectionClass, connectionInfo, new OperationInfo(operationCounter.getNewId()));
RollbackAfterInvokeContext afterContext = null;
try {
interceptor.preInvoke(context);
interceptor.beforeInvoke(beforeContext);

this.connection.rollback();

afterContext = new RollbackAfterInvokeContext();

} catch (SQLException | RuntimeException e) {
context.setException(e);
afterContext = new RollbackAfterInvokeContext(e);
throw e;
} finally {
interceptor.postInvoke(context);
interceptor.afterInvoke(beforeContext, afterContext);
}
}

@Override
public void close() throws SQLException {
Interceptor<CloseContext> interceptor = interceptorFactory.createCloseInterceptor();
CloseContext context = new CloseContext(this.connection, connectionInfo, new OperationInfo(operationCounter.getAndIncrement()));

Interceptor<CloseBeforeInvokeContext, CloseAfterInvokeContext> interceptor = interceptorFactory.createCloseInterceptor();
CloseBeforeInvokeContext beforeContext = new CloseBeforeInvokeContext(this.connection, this.connectionClass, connectionInfo, new OperationInfo(operationCounter.getNewId()));
CloseAfterInvokeContext afterContext = null;
try {
interceptor.preInvoke(context);

interceptor.beforeInvoke(beforeContext);

this.connection.close();

afterContext = new CloseAfterInvokeContext();

} catch (SQLException | RuntimeException e) {
context.setException(e);
afterContext = new CloseAfterInvokeContext(e);
throw e;
} finally {
interceptor.postInvoke(context);
interceptor.afterInvoke(beforeContext, afterContext);
this.resourceMonitor.releaseResource();
}
}
Expand Down
Loading

0 comments on commit bd0864d

Please sign in to comment.