public interface Session
A Session brings together some useful methods and data for the current
database session. It provides a set of attributes (a
String to Object map). Until PL/Java 1.2.0, its attribute
store had transactional behavior (i.e., the data
added since the last commit would be lost on a transaction rollback, or kept
after a commit), but in 1.2.0 and later, it has not, and has functioned as a
Map with no awareness of transactions. Java already provides those,
so the attribute-related methods of Session are now deprecated.
TransactionListeners and SavepointListeners are available for
use by any code that needs to synchronize some state with PostgreSQL
transactions.- Author:
- Thomas Hallgren
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddSavepointListener(SavepointListener listener) Adds the specifiedlistenerto the list of listeners that will receive savepoint events.voidaddTransactionListener(TransactionListener listener) Adds the specifiedlistenerto the list of listeners that will receive transaction events.voidexecuteAsOuterUser(Connection conn, String statement) Execute a statement as the outer user rather than the effective user.voidexecuteAsSessionUser(Connection conn, String statement) Deprecated, for removal: This API element is subject to removal in a future version.Returns an unmodifiable defensive copy of the Javasystem propertiestaken early in PL/Java startup before user code has an opportunity to write them.getAttribute(String attributeName) Deprecated, for removal: This API element is subject to removal in a future version.Session's attribute store once had a special, and possibly useful, transactional behavior, but since PL/Java 1.2.0 it has lacked that, and offers nothing you don't get with an ordinaryMap(that forbids nulls).<T extends PooledObject>
ObjectPool<T> getObjectPool(Class<T> cls) Return an object pool for the given class.Return the outer database user name.Deprecated, for removal: This API element is subject to removal in a future version.As of 1.5.0, this method is retained only for compatibility with old code, and returns the same value asgetOuterUserName, which should be used instead.Return the current effective database user name.voidremoveAttribute(String attributeName) Deprecated, for removal: This API element is subject to removal in a future version.Session's attribute store once had a special, and possibly useful, transactional behavior, but since PL/Java 1.2.0 it has lacked that, and offers nothing you don't get with an ordinaryMap(that forbids nulls).voidremoveSavepointListener(SavepointListener listener) Removes the specifiedlistenerfrom the list of listeners that will receive savepoint events.voidremoveTransactionListener(TransactionListener listener) Removes the specifiedlistenerfrom the list of listeners that will receive transaction events.voidsetAttribute(String attributeName, Object value) Deprecated, for removal: This API element is subject to removal in a future version.Session's attribute store once had a special, and possibly useful, transactional behavior, but since PL/Java 1.2.0 it has lacked that, and offers nothing you don't get with an ordinaryMap(that forbids nulls).
-
Method Details
-
frozenSystemProperties
Properties frozenSystemProperties()Returns an unmodifiable defensive copy of the Javasystem propertiestaken early in PL/Java startup before user code has an opportunity to write them.When PL/Java is running without security policy enforcement, as on stock Java 24 and later, using the frozen properties can simplify defensive coding against the possibility of arbitrary property modifications.
- Returns:
- a
Propertiesobject that departs from the API spec by throwingUnsupportedOperationExceptionfrom any method if the properties would otherwise be modified.
-
addSavepointListener
Adds the specifiedlistenerto the list of listeners that will receive savepoint events. AnAccessControlContextsaved by this method will be used when the listener is invoked. If the listener was already registered, it remains registered just once, though theAccessControlContextis updated and its order of invocation relative to other listeners may change.- Parameters:
listener- The listener to be added.
-
addTransactionListener
Adds the specifiedlistenerto the list of listeners that will receive transaction events. AnAccessControlContextsaved by this method will be used when the listener is invoked. If the listener was already registered, it remains registered just once, though theAccessControlContextis updated and its order of invocation relative to other listeners may change.- Parameters:
listener- The listener to be added.
-
getAttribute
Deprecated, for removal: This API element is subject to removal in a future version.Session's attribute store once had a special, and possibly useful, transactional behavior, but since PL/Java 1.2.0 it has lacked that, and offers nothing you don't get with an ordinaryMap(that forbids nulls). If some kind of store with transactional behavior is needed, it should be implemented in straight Java and kept in sync by using aTransactionListener.Obtain an attribute from the current session.- Parameters:
attributeName- The name of the attribute- Returns:
- The value of the attribute
-
getObjectPool
Return an object pool for the given class.- Parameters:
cls- The class of object to be managed by this pool. It must implement the interfacePooledObjectand have an accessible constructor for one argument of typeObjectPool.- Returns:
- An object pool that pools object of the given class.
-
getUserName
String getUserName()Return the current effective database user name.Definition: "The one to use for all normal permissions-checking purposes." Within
SECURITY DEFINERfunctions and some specialized commands, it can be different from the outer user name. -
getOuterUserName
String getOuterUserName()Return the outer database user name.Definition: "the current user ID in effect at the 'outer level' (outside any transaction or function)." The session user id taking into account any
SET ROLEin effect. This is the ID that aSECURITY DEFINERfunction should revert to if it needs to operate with the invoker's permissions.- Since:
- 1.5.0
-
getSessionUserName
Deprecated, for removal: This API element is subject to removal in a future version.As of 1.5.0, this method is retained only for compatibility with old code, and returns the same value asgetOuterUserName, which should be used instead. Previously, it returned the session ID unconditionally, which is incorrect for any PostgreSQL version newer than 8.0, because it was unaware ofSET ROLEintroduced in 8.1. Any actual use case for a method that ignores roles and reports only the session ID should be reported as an issue.Deprecated synonym forgetOuterUserName. -
executeAsOuterUser
Execute a statement as the outer user rather than the effective user. This is useful when functions declared usingSECURITY DEFINERwants to give up the definer rights.- Parameters:
conn- The connection used for the executionstatement- The statement to execute- Throws:
SQLException- if something goes wrong when executing.- See Also:
-
executeAsSessionUser
@Deprecated(since="1.5.0", forRemoval=true) void executeAsSessionUser(Connection conn, String statement) throws SQLException Deprecated, for removal: This API element is subject to removal in a future version.As of 1.5.0, this method is retained only for compatibility with old code, and has the same effect asexecuteAsOuterUser, which should be used instead. Previously, it used the session ID unconditionally, which is incorrect for any PostgreSQL version newer than 8.0, because it was unaware ofSET ROLEintroduced in 8.1. Any actual use case for a method that ignores roles and uses only the session ID should be reported as an issue.Deprecated synonym forexecuteAsOuterUser.- Throws:
SQLException
-
removeAttribute
Deprecated, for removal: This API element is subject to removal in a future version.Session's attribute store once had a special, and possibly useful, transactional behavior, but since PL/Java 1.2.0 it has lacked that, and offers nothing you don't get with an ordinaryMap(that forbids nulls). If some kind of store with transactional behavior is needed, it should be implemented in straight Java and kept in sync by using aTransactionListener.Remove an attribute previously stored in the session. If no attribute is found, nothing happens.- Parameters:
attributeName- The name of the attribute.
-
removeSavepointListener
Removes the specifiedlistenerfrom the list of listeners that will receive savepoint events. This method does nothing unless the listener is found.- Parameters:
listener- The listener to be removed.
-
removeTransactionListener
Removes the specifiedlistenerfrom the list of listeners that will receive transaction events. This method does nothing unless the listener is found.- Parameters:
listener- The listener to be removed.
-
setAttribute
Deprecated, for removal: This API element is subject to removal in a future version.Session's attribute store once had a special, and possibly useful, transactional behavior, but since PL/Java 1.2.0 it has lacked that, and offers nothing you don't get with an ordinaryMap(that forbids nulls). If some kind of store with transactional behavior is needed, it should be implemented in straight Java and kept in sync by using aTransactionListener.Set an attribute to a value in the current session.- Parameters:
attributeName-value-
-
executeAsOuterUser, which should be used instead.