Skip to content

Commit 667b5c3

Browse files
committed
Update documentation for Open Session in View
Close #81
1 parent 62404ca commit 667b5c3

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

docs/src/main/asciidoc/using.adoc

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,65 @@ When enabled, it publishes events - `JdbcQueryExecutionEvent` and `JdbcMethodExe
7272
== Limitations
7373
[[using-limitations-open-session-in-view]]
7474
=== Open Session In View
75-
Unfortunately, Open Session In View (OSIV) is not supported.
76-
This is because OSIV delays closing the database connection until the HTTP response is sent.
75+
When working with Open-In-View (Open Session In View (OSIV) or Open EntityManager In View (OEIV)), special care is required.
76+
This is because OIV delays closing the database connection, which can affect the boundaries of observations.
7777

78-
Observation scope orders without OSIV:
78+
For example, consider the following code:
7979

80-
. Open HTTP request observation scope
81-
. Open DB connection observation scope
82-
. (Other observation scopes)
83-
. Close DB connection observation scope
84-
. Close HTTP request observation scope
80+
[source,java]
81+
----
82+
@GetMapping("/")
83+
List<Car> cars() {
84+
return Observation.createNotStarted("my.observation", registry).observe(() -> {
85+
return repository.findAll(); // DB access
86+
});
87+
}
88+
----
89+
90+
With OIV *disabled*, the observation interaction looks like this:
8591

86-
However, with OSIV enabled, the DB connection closing is delayed, leading to the following order:
92+
----
93+
Action | Name
94+
----------------------|--------------------
95+
START Observation | "http.server.requests"
96+
OPEN Scope | "http.server.requests"
97+
START Observation | "my.observation"
98+
OPEN Scope | "my.observation"
99+
START Observation | "jdbc.connection"
100+
OPEN Scope | "jdbc.connection"
101+
... | ...
102+
CLOSE Scope | "jdbc.connection"
103+
STOP Observation | "jdbc.connection"
104+
CLOSE Scope | "my.observation"
105+
STOP Observation | "my.observation"
106+
CLOSE Scope | "http.server.requests"
107+
STOP Observation | "http.server.requests"
108+
----
109+
110+
With OIV *enabled*, the order changes:
111+
112+
----
113+
Action | Name
114+
----------------------|--------------------
115+
START Observation | "http.server.requests"
116+
OPEN Scope | "http.server.requests"
117+
START Observation | "my.observation"
118+
OPEN Scope | "my.observation"
119+
START Observation | "jdbc.connection"
120+
OPEN Scope | "jdbc.connection"
121+
... | ...
122+
CLOSE Scope | "my.observation" <==
123+
STOP Observation | "my.observation" <==
124+
CLOSE Scope | "jdbc.connection" <==
125+
STOP Observation | "jdbc.connection" <==
126+
CLOSE Scope | "http.server.requests"
127+
STOP Observation | "http.server.requests"
128+
----
87129

88-
. Open HTTP request observation scope
89-
. Open DB connection observation scope
90-
. (Other observation scopes)
91-
. *(SWAPPED)* Close HTTP request observation scope
92-
. *(SWAPPED)* Close DB connection observation scope
130+
As you can see, when OIV is enabled, the `jdbc.connection` observation/scope is closed *after* the `my.observation` observation/scope.
131+
Since scopes must be closed in the reverse order of their opening, this swapped order can cause leaks in thread-local bound values (such as spans), because opening and closing scopes trigger thread-local operations.
93132

94-
Since observation scopes must be closed in the reverse order of their creation, this swapped ordering causes observation leaks.
133+
The example above uses an explicitly created observation, but the same applies if you use the `@Observed` annotation or another instrumentation library.
134+
Therefore, when OIV is enabled, pay careful attention to observation boundaries — especially if the observation includes database access.
95135

96-
To disable OSIV, set `spring.jpa.open-in-view=false`.
136+
In upcoming versions, we plan to narrow the scope of connection observations to better support the OIV pattern.

0 commit comments

Comments
 (0)