我编写了一个 C# 应用程序,它与部署在另一台机器上的 (ASMX) Web 服务进行通信。Web 服务连接到位于第三层的后端数据库并对其执行操作。
应用程序中的所有数据库操作都是从名为DataLayerFunctor
. 这是该课程的一个片段:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebserviceTest.DataLayer;
using WebserviceTest.SecurityLayer;
using SettingsAlias = WebserviceTest.Properties;
namespace WebserviceTest
{
public static class DataLayerFunctor
{
public static MyWebserviceReference.Service1 myWebService;
private static string HOST = "192.168.1.100";
private static string PORT = "1521";
private static string DATABASE = "orcl";
private static string USERNAME = "MY_USER";
private static string PASSWORD = "123";
private static string ORACLE_CONNECTION_STRING = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={2})));Users Id={3};Password={4};";
public static List<UserGroup> UserGroupsList { get; set; }
public static List<Role> RolesList { get; set; }
public static Dictionary<UserGroup,Role> GroupsToRoles { get; set; }
static DataLayerFunctor()
{
HOST = CryptoServices.DecryptText(SettingsAlias.Settings.Default.HOST,CryptoServices.DEFAULT_KEY) ?? HOST;
PORT = CryptoServices.DecryptText(SettingsAlias.Settings.Default.PORT,CryptoServices.DEFAULT_KEY )?? PORT;
DATABASE = CryptoServices.DecryptText(SettingsAlias.Settings.Default.DATABASE,CryptoServices.DEFAULT_KEY) ?? DATABASE;
USERNAME = CryptoServices.DecryptText(SettingsAlias.Settings.Default.USER_NAME,CryptoServices.DEFAULT_KEY) ?? USERNAME;
PASSWORD = CryptoServices.DecryptText(SettingsAlias.Settings.Default.PASSWORD,CryptoServices.DEFAULT_KEY) ?? PASSWORD;
ORACLE_CONNECTION_STRING = SettingsAlias.Settings.Default.ORACLE_CONNECTION_STRING ?? ORACLE_CONNECTION_STRING;
HOST = "192.168.1.6";
PORT = "1521";
DATABASE = "orcl";
USERNAME = "ALAMAL_BANK";
PASSWORD = "123";
myWebService = new MyWebserviceReference.Service1();
myWebService.Url = "http://192.168.1.6/MyWebservice/Service1.asmx";
//myWebService.Url = CryptoServices.DecryptText(SettingsAlias.Settings.Default.WebserviceURL,CryptoServices.DEFAULT_KEY);
myWebService.Timeout = 36000;
//Load enumeration tables
LoadGroupsToRoles();
}
public static void LoadGroupsToRoles() {
string query = "SELECT * FROM GROUPS_TO_ROLES";
DataTable groupsToRoles = myWebService.GetTableParamOracle(query, HOST, PORT, DATABASE, USERNAME, PASSWORD);
GroupsToRoles = new Dictionary<UserGroup, Role>();
foreach (DataRow groupsToRolesRow in groupsToRoles.Rows)
{
Role role = RolesList.First((i) => i.RoleId == groupsToRolesRow["ROLE_ID_FK"] as long?);
UserGroup userGroup = UserGroupsList.First((i) => i.GroupId == groupsToRolesRow["GROUP_ID_FK"] as long?);
GroupsToRoles.Add(userGroup, role);
}
}
}
}
问题是第一次连接到数据库总是失败。因此,我需要重新运行应用程序以获得部署应用程序时无法接受的连接。另一件事是连接会在几分钟后重置。我怎样才能保持连接活跃?
我正在使用:
ORACLE 11g DB
ODP.NET
C# 4
SOAP Webservice
网络服务代码:
public class Service1 : System.Web.Services.WebService
{
logger.LogCreator _log = new logger.LogCreator(@"C:\GDW_logs");
public static long DataTableCounter = 1;
string oracleConnection = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={2})));User Id={3};Password={4};";
[WebMethod]
public DataTable GetTableParamOracle(string sqltext, string host, string port, string database, string username, string password)
{
OracleConnection SQLConnection = new OracleConnection(string.Format(oracleConnection, host, port, database, username, password));
OracleDataAdapter dad = new OracleDataAdapter(sqltext, SQLConnection);
DataTable dtb = new DataTable("DataTable" + (DataTableCounter++));
SQLConnection.Open();
try
{
dad.Fill(dtb);
}
catch (Exception ex)
{
_log.WriteLine(ex.Message);
}
finally
{
SQLConnection.Close();
}
return dtb;
}
}