# Sessions

For interacting with Tempus Serva records you need a session.  
You can create one like this:

```java
Session session = SessionFactory.getSession(this);
// or, in a codeunit
Session session = getSession();
```

All sessions need to be terminated after use (releasing DB connections etc.)

```java
session.close();
// or in a finally (tests for NPE)
DbClose.close(session);
// or use try-with
try (Session session = SessionFactory.getSession(this)) {
} catch (TsCloseObjectRequired ignore) {}
```

All sessions are automatically closed after Policy#apiSessionMaxLifetime (default is 30 seconds).  
If you suspect your code might be running for more than that, the session can be marked as *long running*.  
This can be done durring the initialization of the session, or after the fact.

```java
Session session = SessionFactory.getSession(context, this, 30);
// or, in a codeunit
Session session = getSession(30);
// or, after the fact
session.setLongRunningSession(30);
```

The above code extends the lifetime of the session by 30 seconds.

If you forget to close a session, an entry will be added to the eventlog, like this:

`[ClassName] probably forgot to close session`