public class SFSDBManager
extends java.lang.Object
Each Zone runs its own DbManager which can be configured via the Zone Configurator module in the SFS2X AdminTool. Additionally a Zone can instantiate multiple DbManagers via server side code. A typical scenario for this is when the application requires to connect to multiple databases.
DBConfig| Modifier and Type | Field and Description |
|---|---|
protected boolean |
active |
protected DBConfig |
config |
protected org.slf4j.Logger |
log |
protected java.lang.String |
name |
protected Zone |
parentZone |
| Constructor and Description |
|---|
SFSDBManager(DBConfig config) |
| Modifier and Type | Method and Description |
|---|---|
void |
destroy(java.lang.Object o)
Destroy service
|
java.lang.Object |
executeInsert(java.lang.String sql,
java.lang.Object[] params)
Executes a SQL INSERT command returning the key of the inserted row
|
ISFSArray |
executeQuery(java.lang.String sql)
This is a small variation on
IDBManager.executeQuery(String, Object[])
where no additional SQL parameter is used. |
ISFSArray |
executeQuery(java.lang.String sql,
java.lang.Object[] params)
Perform a SQL query and return a structured object based on SFSArray and SFSObject.
|
void |
executeUpdate(java.lang.String sql)
Executes a non-query SQL command such as INSERT, UPDATE, DELETE etc...
|
void |
executeUpdate(java.lang.String sql,
java.lang.Object[] params)
Executes a non-query SQL command such as INSERT, UPDATE, DELETE etc...
|
int |
getActiveConnections()
Get the number of pooled connections currently active
|
DBConfig |
getConfig()
Get the configuration details of the JDBC connection and connection pool
|
java.sql.Connection |
getConnection()
Get a direct reference to the JDBC connection object.
|
int |
getIdleConnections()
Get the number of pooled connections currently idle
|
java.lang.String |
getName()
Get the service name
|
void |
handleMessage(java.lang.Object arg0)
Send message to service
|
void |
init(java.lang.Object o)
Initialize service
|
boolean |
isActive()
True if the Service is active
|
void |
setName(java.lang.String name)
Set the service name
|
protected Zone parentZone
protected boolean active
protected final DBConfig config
protected final java.lang.String name
protected final org.slf4j.Logger log
public SFSDBManager(DBConfig config)
config - the configuration settings for the JDBC connection and connection poolpublic void init(java.lang.Object o)
com.smartfoxserver.bitswarm.service.IServiceinit in interface com.smartfoxserver.bitswarm.service.IServiceo - custom parameterspublic void destroy(java.lang.Object o)
com.smartfoxserver.bitswarm.service.IServicedestroy in interface com.smartfoxserver.bitswarm.service.IServiceo - custom parameterspublic java.sql.Connection getConnection()
throws java.sql.SQLException
An example of a code template would be:
try
{
// An example query ... it could be anything
sql = "SELECT * FROM table";
conn = getParentZone().getDBManager().getConnection();
stmt = conn.prepareStatement(sql);
ResultSet resultSet = stmt.executeQuery();
// More code here...
}
// Not mandatory
catch (SQLException)
{
// do something about it
}
// Mandatory! Close connection before leaving this method
finally
{
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
}
java.sql.SQLExceptionpublic ISFSArray executeQuery(java.lang.String sql) throws java.sql.SQLException
IDBManager.executeQuery(String, Object[])
where no additional SQL parameter is used. Please see IDBManager.executeQuery(String, Object[])sql - the SQL codejava.sql.SQLException - reports any errors related with the execution of the SQL querypublic ISFSArray executeQuery(java.lang.String sql, java.lang.Object[] params) throws java.sql.SQLException
The SQL code can include placeholders (using a question mark) and an array of parameters that will be used to populate them, just like when using prepared statements via JDBC. Example:
executeQuery("SELECT * FROM Users WHERE age > ? AND country=?", new Object[] {35, "Sweden"});
The structure of the returned object is as follows:
SFSArray: represents the result set. It contains all the selected records in form of SFSObject(s)
Data types from the database are translated to SFSObject types according to this table:
| SQL Type | SFSObject Type |
| NULL | NULL |
| BOOLEAN | BOOLEAN |
| DATE | LONG (Unix timestamp) |
| FLOAT, DECIMAL, DOUBLE, REAL | DOUBLE |
| TINYINT, SMALLINT, INTEGER | INTEGER |
| CHAR, VARCHAR, LONGVARCHAR | UTF_STRING |
| NCHAR, NVARCHAR, LONGNVARCHAR | UTF_STRING |
| TIMESTAMP | LONG |
| BIGINT (up to 2^63) | LONG |
| LONGVARBINARY, BLOB | BYTE_ARRAY |
sql - the SQL code. Placeholders for parameters can be used such as: SELECT * FROM Users WHERE name=?params - An array of objects that will be used to populate the placeholders in the SQL codejava.sql.SQLException - reports any errors related with the execution of the SQL querypublic void executeUpdate(java.lang.String sql)
throws java.sql.SQLException
sql - the SQL code.java.sql.SQLException - reports any errors related with the execution of the SQL updatepublic void executeUpdate(java.lang.String sql,
java.lang.Object[] params)
throws java.sql.SQLException
sql - the SQL code. Placeholders for parameters can be used such as: SELECT * FROM Users WHERE name=?params - An array of objects that will be used to populate the placeholders in the SQL codejava.sql.SQLException - reports any errors related with the execution of the SQL updatepublic java.lang.Object executeInsert(java.lang.String sql,
java.lang.Object[] params)
throws java.sql.SQLException
sql - the SQL code. Placeholders for parameters can be used such as: INSERT INTO users (name, email) VALUES(?, ?)params - An array of objects that will be used to populate the placeholders in the SQL codejava.sql.SQLException - reports any errors related with the execution of the SQL updatepublic int getActiveConnections()
public int getIdleConnections()
public java.lang.String getName()
com.smartfoxserver.bitswarm.service.IServicegetName in interface com.smartfoxserver.bitswarm.service.IServicepublic void setName(java.lang.String name)
com.smartfoxserver.bitswarm.service.IServicesetName in interface com.smartfoxserver.bitswarm.service.IServicename - the service namepublic void handleMessage(java.lang.Object arg0)
com.smartfoxserver.bitswarm.service.IServicehandleMessage in interface com.smartfoxserver.bitswarm.service.IServicearg0 - the messagepublic DBConfig getConfig()
IDBManagergetConfig in interface IDBManagerDBConfigpublic boolean isActive()
IDBManagerisActive in interface IDBManager