cancel
Showing results for 
Search instead for 
Did you mean: 

SAP.Connector.SAPConnection

Former Member
0 Kudos

Hi,

I am using RFC to call an sap function. My code is;

SAP.Connector.Destination destination = new ...

//do destination config...

SAP.Connector.SAPConnection sapConnection = new SAP.Connector.SAPConnection(destination);

sapProxy = new SAPProxy();

sapProxy.Connection = sapConnection;

<b>sapConnection.Open();</b>

sapProxy.Z_CALL_MY_FUNCTION(...);

There is no problem calling a function. I need that I should not close the connection.

I want to learn whether proxy closes the connection after function is called or not? I realized that if I do not open the connection proxy opens the connection and calls the function. But is the same position is valid for closing connection? Is connection closed by proxy after function is called?

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

reiner_hille-doering
Active Contributor
0 Kudos

The proxy will close the connection, but not after a call but only if it is disposed:

sapProxy.Dispose(); //closes (disposes) sapProxy.Connection;

You can also reuse the connection, e.g. with onother proxy:

Connection conn = sapProxy.Connection;

sapProxy.Connection = null;

...

SAPProxy2 proxy2 = new SAPProxy2();

proxy2.Connection = conn;

The same is true for the ConnectionPool:

SAP.Connector.Connection.ReturnConnection(sapProxy.Connection):

sapProxy.Connection = null;

...

SAPProxy2 proxy2 = new SAPProxy2();

proxy2.Connection = SAP.Connector.Connection.GetConnectionFromPool(...);

If you forget to close the connection, it will be automatically closed by its finalizer when it is deleted by the Garbage Collector. However it's advisable to explicitly define the connection's live time.

Former Member
0 Kudos

Thanks Reiner,

Your answer is so helpful. After I called my function using proxy, I am saving the proxy in cache, also saving the connection and other resources that proxy needs. I am not expilictly calling the dispose method. Does my this solution cause any problem?

I want to ask your sentence that "However it's advisable to explicitly define the connection's live time."

How can I define the connection time? If I do not define connection time, is it meaning that a connection will be closed after a default time?

Thanks.

reiner_hille-doering
Active Contributor
0 Kudos

Your solution works, but it could happen that you keep the connection(s) longer than necessary. A good alternative is to use the connection pool, as it allows to define parameters like maximum idle time, maximum number of open connections and so forth. Please see online documentation, classes Config and ConnectionPoolSettings for details.

Here some snippets how you could do it:

SAPProxy GetProxyFromCache()

{

SAPProxy proxy = hashtale[criterium];

proxy.Connection = SAP.Connector.Connection.GetConnectionFromPool(this.destination);

return proxy;

}

void PutProxyToCache(SAPProxy proxy)

{

SAP.Connector.Connection.ReturnConnection(proxy.Connection);

proxy.Connection = null;

hashtable[crtierium] = proxy;

}

Former Member
0 Kudos

Thanks Reiner.

Answers (0)