Class Connection
Class that stablish a connection with Kivi-direct to use the KiVi API.
The library support only one physical connection, although multiple virtual connections can be active at the same time but only if all of them are connected to the same server and with the same kivi-user, session provider and same environment. Therefore, it is needed to reset the connection to the server before connecting to another Leanxcale instance or with a different kivi-user. reset()
To create a Connection instance use connect(String). The connection string has the following format:
lx://<ip>:<port>/<database>[@<schema>][;user=<user>][;schema=<schema>]
[;kvnat=<auto or nat address>][;kvproxy=<no or ip!port>][;cert=<path to certificates>]
[;kvmsaddr=<ip!port when lxis and not the default>]
Where:
- kiviConnect through Kivi direct
- kvmsUse basic session provider
- lxisUse external session provider
- ip:portKivi metadata address when using basic session provider, or external session provider address when lxis. In this latter case, the default Kivi metaddata address is the same host on port 14400. This can be overriden by KVMSADDR property.
- databaseThe database name to connect to.
- user propertyUser id to connect with
- schema property [Opt]Optional schema to connect to. It is the user id by default
- kvmsaddr property [Opt]kvms address to override default kvms address when connecting to an external session provider
- kvnat property [Opt]Nat address used to connect to Kivi
- kvproxy property [Opt]Proxy address with format 'ip!port' to connect to Kivi
- kvuser property [Opt]Path to kivi certificates to secure the connectiong
In order to interact with Leanxcale, it is needed a session provider that will age every session along all the datastore nodes. This API supports two connections modes: a basic one that is based on the session timestamp, and an external provider, that will provide the age for every new session. To use the external one, use the 'lxis' mode on connection string when calling connect(String).
This class allows also to list all the tables in the Connection. tables()
It also provides a couple of method for debugging as debug(String) and lastError().
Inheritance
Implements
Inherited Members
Namespace: lxapi
Assembly: lxapi.dll
Syntax
public class Connection : IDisposable
Examples
Connect to a Leanxcale node with user "user1" which metadata server is running at ip "10.8.0.11" and port "14400", and its external provider is running at the same address and default port "9876". Also in this example, network logs are activated before dialing in order to find out any network problem. Finally, it prints the list of database tables before cleaning and connecting to another node.
Connection.debug("+1DN");
using (Connection conn = Connection.connect("lx://"10.8.0.11:9876/db1@user1"))
{
List <string> tables = Connection.tables();
foreach (string tname in tables)
{
Console.WriteLine("Table {0}", tname);
}
}
Connection.reset();
Connection conn2 = "lx://138.2.1.37:14400/db2;user=user2";
Fields
connectionStr
Connection string
Declaration
public readonly string connectionStr
Field Value
Type | Description |
---|---|
System.String |
Methods
connect(String)
Create a connection to the desired Leanxcale node and database
Declaration
public static Connection connect(string connstr)
Parameters
Type | Name | Description |
---|---|---|
System.String | connstr | connection string |
Returns
Type | Description |
---|---|
Connection | New connection |
Examples
Connect to a Leanxcale node with user "user1" which metadata server is running at ip "10.8.0.11" and default port "14400", and its external provider is running at the same address and at port "9876".
Second connection is on metadata server at ip "138.2.1.37" and port "14400". Not using an external session provider
Also in this example, network logs are activated before dialing in order to find out any network problem. Finally, it prints the list of database tables before cleaning and connecting to another node.
Connection.debug("+1DN");
using (Connection conn = Connection.connect("lx://"10.8.0.11:9876/db1@user1"))
{
List <string> tables = Connection.tables();
foreach (string tname in tables)
{
Console.WriteLine("Table {0}", tname);
}
}
Connection.reset();
Connection conn2 = Connection.connect("lx://138.2.1.37:14400/db2;user=user2");
debug(String)
For debugging, takes a debug flag string, updates debug flags, and returns debug flags in use. Debug flags are named using letters. If it starts with a plus sign, flags that follow are added (or their level increased). A minus sign before the flags clears them. Otherwise, debug flags are set exactly to the ones given.
Declaration
public static string debug(string what)
Parameters
Type | Name | Description |
---|---|---|
System.String | what | debug flag string |
Returns
Type | Description |
---|---|
System.String | current debug flag string |
Dispose()
Dispose
Declaration
public void Dispose()
Dispose(Boolean)
Release Connection Handle
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing |
Finalize()
Connection destructor
Declaration
protected void Finalize()
forceconnect(String)
Force creating a connection to the desired Leanxcale node and database.
This is equivalent to the following code
if (isActive("user2", "138.2.1.37", "14400", "9876", "138.2.1.50"))
{
reset();
}
Conn conn = connect("user2", "138.2.1.37", "14400", "db2", "user1", "9876", "138.2.1.50");
Declaration
public static Connection forceconnect(string connstr)
Parameters
Type | Name | Description |
---|---|---|
System.String | connstr | connection string |
Returns
Type | Description |
---|---|
Connection | New connection |
Examples
Force connecting to a Leanxcale node with user "user1" which metadata server is running at ip "10.8.0.11" and defaulr port "14400", and its external provider is running at port "9876". Also in this example, network logs are activated before dialing in order to find out any network problem. Finally, it prints the list of database tables before forcing to create a connection to another node.
using (Connection conn = Connection.forceconnect("lx://"10.8.0.11:9876/db1@user1"))
{
List <string> tables = Connection.tables();
foreach (string tname in tables)
{
Console.WriteLine("Table {0}", tname);
}
}
Connection conn2 = Connection.forceconnect("lx://138.2.1.37:14400/db2@user2");
getHandle()
Return connection's handle
Declaration
public IntPtr getHandle()
Returns
Type | Description |
---|---|
System.IntPtr |
isActive()
Check if the connection is Active
Declaration
public bool isActive()
Returns
Type | Description |
---|---|
System.Boolean | true when active, false in other case |
isActive(String)
Check if the connection to the desired server is the Active one
Declaration
public static bool isActive(string connstr)
Parameters
Type | Name | Description |
---|---|---|
System.String | connstr | connection string |
Returns
Type | Description |
---|---|
System.Boolean | True when connected to a compatible connections tring. False in other case |
Examples
Reset the library's connection in case it is not the desired one and then connect to the desired server.
if (isActive("lx://138.2.1.37:14400/db2@user2"))
{
reset();
}
Conn conn = connect("lx://138.2.1.37:14400/db2@user2");
lastError()
Retrieve last error message
Declaration
public static string lastError()
Returns
Type | Description |
---|---|
System.String | last error message |
reset()
Reset library. Any Connection with the server and its ongoing sessions will be closed and cancelled.
Declaration
public static void reset()
Exceptions
Type | Condition |
---|---|
LxException | Thrown when connection was already closed. |
session()
Create and begin a session.
Declaration
public Session session()
Returns
Type | Description |
---|---|
Session | new session |
tableInfo(String)
Retrieve table's info from database connection
Declaration
public TableInfo tableInfo(string tname)
Parameters
Type | Name | Description |
---|---|---|
System.String | tname | table name |
Returns
Type | Description |
---|---|
TableInfo | table's info |
tables()
List database tables
Declaration
public List<string> tables()
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.String> | List of database tables |
typeName(LxTypes)
Get the field type's name
Declaration
public static string typeName(LxTypes typ)
Parameters
Type | Name | Description |
---|---|---|
LxTypes | typ | kivi type LxTypes |
Returns
Type | Description |
---|---|
System.String | field type name |