Untitled
unknown
plain_text
2 years ago
2.8 kB
15
Indexable
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class ConnectionLoggingAspect {
@Before("execution(* your.package.repository.*.*(..))")
public void logConnectionInfo(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
if (arg instanceof javax.persistence.EntityManager) {
// Получите EntityManager и извлеките информацию о соединении
javax.persistence.EntityManager entityManager = (javax.persistence.EntityManager) arg;
javax.persistence.EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
if (entityManagerFactory instanceof org.hibernate.internal.SessionFactoryImpl) {
org.hibernate.engine.spi.SessionFactoryImplementor sessionFactory =
(org.hibernate.engine.spi.SessionFactoryImplementor) entityManagerFactory;
// Получите информацию о соединении
org.hibernate.engine.jdbc.connections.spi.ConnectionProvider connectionProvider =
sessionFactory.getServiceRegistry().getService(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider.class);
if (connectionProvider instanceof org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl) {
javax.sql.DataSource dataSource = ((org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl) connectionProvider).getDataSource();
// Теперь у вас есть доступ к DataSource, и вы можете получить информацию о соединении
String dbConnectionId = getDbConnectionId(dataSource);
System.out.println("DB Connection ID: " + dbConnectionId);
}
}
}
}
}
private String getDbConnectionId(javax.sql.DataSource dataSource) {
// Реализуйте логику извлечения идентификатора соединения для вашей конкретной базы данных
// Например, для MySQL это может быть что-то вроде: return dataSource.getConnection().createStatement().executeQuery("SELECT CONNECTION_ID()").getString(1);
return null;
}
}
Editor is loading...
Leave a Comment