How to close a connection to database server with JS macro?

Hi,

I found that my JS macros created many connections to postgresql database and sometimes the server send error: “FATAL: sorry, too many clients already”
May be I forget to close connection before to create new one.
It is to long to explain here how I did to make connection but I use somewhere oXInterface.oXDriverManager.getConnectionWithInfo(…)
What are functions to close a connection to the server ? please send me an example code.

Thanks

Are you running macros in the context of forms? Then re-use the existing connections established by the loaded form instead of opening new connections.

Yes my macro is used in form.

Starting form a form:

oConnection = oForm.activeConnection()

Starting to get a connection if it doesn’t exist:

oDatasource = thisDatabaseDocument.CurrentController
IF NOT (oDatasource.isConnected()) THEN oDatasource.connect()
oConnection = oDatasource.ActiveConnection()

Why do you open more than one connection to one Base file?

There are several forms. Each time I open a form a new connection occurred. My code lack to use the current connection. Thanks for all your help.

Sub any_control_event(ev)
oModel = ev.Source.Model
frm = getParentObject(oModel, "com.sun.star.form.component.DataForm")
con = frm.ActiveConnection
End Sub

Sub any_form_event(ev)
con = ev.Source.ActiveConnection
End Sub

Function getParentObject(byval m, srvName$)
on error goto NullObj
	do until m.supportsService(srvName)
		m = m.getParent()
	loop
	getParentObject = m
	exit function
	NullObj:
	getParentObject = Null
End Function

The function can return any parent object up to the containing document. With “com.sun.star.form.component.DataForm” it returns the logical form of a calling form control, even if the caller is a column within a table control.

Method close.

oConnection.close()