简体   繁体   English

Spring 启动 Hibernate 创建连接/会话时运行查询

[英]Spring Boot Hibernate run query when connection/session is created

I have Spring Boot application with implemented RESTController s.我有 Spring 启动应用程序,并实现RESTController In the RESTController I specified /clients/ endpoint ( PUT method) to update client resource.在 RESTController 中,我指定/clients/端点( PUT方法)来更新客户端资源。

In my database (PostgreSQL) I have implemented several triggers on the client table to record history of the changes to the client_history table.在我的数据库(PostgreSQL)中,我在client表上实现了几个触发器来记录对client_history表的更改历史。 The problem is that I need to insert author ( user_id ) of the changes to the client_history table.问题是我需要将更改的作者( user_id )插入到client_history表中。

This question ( https://stackoverflow.com/a/13172964 ) led me to use GUC inside my trigger function.这个问题( https://stackoverflow.com/a/13172964 )导致我在触发器 function 中使用 GUC。
select current_setting('custom.application_user_id'))

Now I need to execute sql query set custom.application_user_id = {user_id};现在我需要执行 sql query set custom.application_user_id = {user_id}; to set user_id when Hibernate opens new session.当 Hibernate 打开新的 session 时设置 user_id。 I have tried to use Hibernate Interceptors to do this, but this solution does not work.我曾尝试使用 Hibernate 拦截器来执行此操作,但此解决方案不起作用。

Could someone give me an example how to subscribe to the event when session is created?有人可以给我一个例子,当 session 创建时如何订阅事件?

PS Also I have silly question: Does every HTTP requests received by the application creates (obtains from connection pool) new connection? PS 另外我有一个愚蠢的问题:应用程序收到的每个 HTTP 请求是否创建(从连接池中获取)新连接?

After research, I found this trick.经过研究,我发现了这个技巧。 For me i have two datasource in my project (ds1, ds2)对我来说,我的项目中有两个数据源(ds1,ds2)

spring:
  datasource:
    ds1:
      url: jdbc:oracle:thin:${DATABASE_USERNAME:E4A5}/${DATABASE_PASSWORD:EA5}@${DATABASE_HOSTNAME:local4host}:${DATABASE_PORT:49161}:${DATABASE_SERVICE:EE}
      driver-class-name: oracle.jdbc.OracleDriver
    ds2:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      url: jdbc:sqlserver://${DATABASE_EF_HOSTNAME:localhost};databaseName=${DATABASE_EF_NAME:test}
      username: ${DATABASE_EF_USERNAME:test}
      password: ${DATABASE_EF_PASSWORD:test}

And to run query when connection/session is created in ds1 I did this in my class annotated with @Configuration:并在 ds1 中创建连接/会话时运行查询,我在使用 @Configuration 注释的 class 中执行了此操作:

@Primary
@Bean
@ConfigurationProperties("spring.datasource.ds1")
public DataSourceProperties ds1DataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
@Primary
@ConfigurationProperties("spring.datasource.ds1.configuration")
public DataSource ds1DataSource() {
    HikariDataSource hikariDataSource = ds1DataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    hikariDataSource.setConnectionInitSql("ALTER SESSION SET NLS_DATE_FORMAT='DD/MM/YYYY' " +
            "NLS_SORT='Binary' " +
            "NLS_NUMERIC_CHARACTERS='.,' " +
            "NLS_LANGUAGE='FRENCH'");
    return hikariDataSource;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM