Log out action method

Viewpoint
Teradata Employee

Log out action method

Hello,

if the user log out from viewpoint, I want to execute a method in my portlet. Is there a way how can I do it? If the user open the portlet, the portlet opens a connection to the db over the class TeradataDataSourceFactory. If the user go out from the portlet, I want to close the connection. I Know I can close and open the connection by execute one or a group of sql statements. I cannot add a connection pool by starting the server, because the properties of the connection will be set over viewpoint.

I know I can use the a javascript method by removing the portlet, but this is not the solution that I´m searching for.

4 REPLIES
Teradata Employee

Re: Log out action method

There are 2 situations in which I would recommend that you close your connection to the database.  The first is when the user logs out, and the second is when the user removes the portlet from his page.  Viewpoint's SQL Scratchpad portlet deals with this problem in the exact same fashion.

To handle the situation where the user logs out of Viewpoint, you should write a Java class that implements the HttpSessionListener interface.  This class should close the connection to the database in the sessionDestroyed method.  This class then needs to be registered in the web.xml file as a listener.

public class PortletSessionListener implements HttpSessionListener {

public void sessionCreated(HttpSessionEvent event) {
// Do nothing
}

public void sessionDestroyed(HttpSessionEvent event) {
// Retrieve and close your connection here
}
}

<!-- Fragment to add to web.xml -->
<listener>
<listener-class>com.teradata.viewpoint.portlets.myportlet.listener.PortletSessionListener</listener-class>
</listener>

The second situation to handle is when the user removes the portlet from the page.  You will want to register a custom callback function in JavaScript that will be triggered when the portlet is removed.  This callback function should make a call to a URL in your portlet which can be responsible for closing the connection to the database. Adding the server side code to expose this URL is something you should already be familiar with, so I will exclude any code samples for that here.  You would want to add some code similar to the following to your summary.jsp page.

<script type="text/javascript">
TDPortalManager.onPortletRemove('${context}', function() {
// Make an AJAX call here to your disconnect URL here
});
</script>

Hope that helps! 

Teradata Employee

Re: Log out action method

Thank you for your reply.

I used the first suggestion and for spring context I used this:

AfbManager afbManager = null;  
 
 HttpSession session = sessionEvent.getSession();
 
        ApplicationContext ctx =
              WebApplicationContextUtils.
                    getWebApplicationContext(session.getServletContext());

        afbManager = (AfbManager) ctx.getBean("afbManager");

Another question:

How can I read from the ControllerContext the Session. I need the sessionId...

Teradata Employee

Re: Log out action method

You have the HttpSession object.  You can get the session ID by calling getId() on this object.

Teradata Employee

Re: Log out action method

Thank you for the reply.

How can I get the PortletSession ID?