问题描述:sqlite3数据放置在某一台电脑的某个共享文件夹下,操作数据库的应用程序安装在同一局域网下的很多台电脑上,由于存在多人同时使用该应用程序,所以存在多人同时操作数据库的情况。经过测试发现,最常见的情况是当两人或者多人往数据库中写入数据时,只有其中一个写入成功,其他数据都写入失败。
解决方案分析:
由于本人编写程序未MFC应用程序,所以尝试使用windows互斥量mutex,具体的使用方法如下:
bool CMFCApplication2Dlg::Lock() { m_pMutex = CreateMutex(NULL, false, L"txt_mutex"); if (NULL == m_pMutex) { return false; } DWORD nRet = WaitForSingleObject(m_pMutex, INFINITE); if (nRet != WAIT_OBJECT_0) { return false; } return true; } bool CMFCApplication2Dlg::UnLock() { return ReleaseMutex(&m_pMutex); }
在某用户开始进行写入操作时,先调用Lock()获取mutxt,写入完成之后调用UnLock()释放mutxt。然并卵,该方法并不奏效。(可能由于本人对windows多线程/多进程编程这一块太过生疏,所以无法利用这方面的知识来解决这个问题,如果有大神知道解决方法,求不吝赐教)
所以经过一番思考之后,决定使用在共享盘的那台电脑上跑一个服务端小程序来防止数据库的操作冲突。
具体实现方法如下:
思路分析:在服务端每收到一个客户端的连接请求之后,都创建一个新的线程来处理相应的操作,新的线程不断的去获取客户端发来的消息,当客户端发来的消息是”开始操作”时,线程将尝试获取互斥量mutxt,获取成功之后将给客户端发送回复消息,当该线程接收到”操作结束”的消息时,线程将释放互斥量mutxt,并且会断开该客户端与服务端的socket连接。
服务端代码:
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <string>
#pragma comment(lib, "ws2_32.lib")
using namespace std; static HANDLE m_mutex = INVALID_HANDLE_VALUE; DWORD WINAPI AnswerThread(LPVOID lparam)
{
SOCKET ClientSocket = (SOCKET)(LPVOID)lparam;
int bytesRecv;
while ()
{
bytesRecv = SOCKET_ERROR;
char sendbuff[] = "ok";
char recvbuf[] = "";
for (int i = ; i<(int)strlen(recvbuf); i++)
{
recvbuf[i] = '\0';
}
while (bytesRecv == SOCKET_ERROR)
{
bytesRecv = recv(ClientSocket, recvbuf, sizeof(recvbuf), );
}
string recved = recvbuf;
if (recved == "op_begin")
{
WaitForSingleObject(m_mutex, INFINITE);
cout << "op_begin" << endl;
send(ClientSocket, sendbuff, sizeof(sendbuff), );
}
if (recved == "op_end")
{
cout << "op_end" << endl;
ReleaseMutex(&m_mutex);
closesocket(ClientSocket);
return ;
}
}
return ;
}
int main()
{
WSADATA wsaData;
int iRet = WSAStartup(MAKEWORD(, ), &wsaData);
if (iRet != NO_ERROR)
printf("Error at WSAStartup()\n"); SOCKET m_socket;
m_socket = socket(AF_INET, SOCK_STREAM, );
if (m_socket == INVALID_SOCKET)
{
printf("Error at socket():%ld\n", WSAGetLastError());
WSACleanup();
return ;
}
SOCKADDR_IN service;
service.sin_family = AF_INET;
service.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
service.sin_port = htons(); if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
{
printf("bind() failed.\n");
closesocket(m_socket);
return ;
}
else
printf("bind ok.\n"); if (listen(m_socket, ) == SOCKET_ERROR)
printf("Error listening on socket.\n");
else
printf("listening ok.\n"); SOCKET AcceptSocket;
printf("waiting for a client to connect...\n");
m_mutex = CreateMutex(NULL, FALSE, L"Mutex");
if (!m_mutex)
{
cout << "Failed to CreateMutex !" << endl;
return ;
}
int count = ;
while ()
{
AcceptSocket = SOCKET_ERROR;
while (AcceptSocket == SOCKET_ERROR)
{
AcceptSocket = accept(m_socket, NULL, NULL);
}
count++;
printf("client num %d connected.\n", count); DWORD dwThreadId;
HANDLE hThread; hThread = CreateThread(NULL, NULL, AnswerThread, (LPVOID)AcceptSocket, , &dwThreadId);
if (hThread == NULL)
{
printf("CreatThread AnswerThread() failed.\n");
}
else
{
printf("create thread %d ok.\n", count);
}
CloseHandle(hThread);
}
closesocket(m_socket);
WSACleanup();
}
客户端代码:(进入数据库操作前)
WSADATA wsaData; if (WSAStartup(MAKEWORD(, ), &wsaData) != ) { TRACE("Failed to load Winsock"); return; } string txtPath = save_path + "\\ip.txt"; ifstream infile(txtPath); string ip; getline(infile, ip); infile.close(); SOCKADDR_IN addrSrv; addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(); addrSrv.sin_addr.S_un.S_addr = inet_addr(ip.c_str()); sockClient = socket(AF_INET, SOCK_STREAM, ); if (SOCKET_ERROR == sockClient){ TRACE("Socket() error:%d", WSAGetLastError()); return; } if (connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(addrSrv)) == INVALID_SOCKET){ TRACE("Connect failed:%d", WSAGetLastError()); return; } char buff[] = "op_begin"; send(sockClient, buff, sizeof(buff), ); int bytesRecv = SOCKET_ERROR; char recvbuf[] = ""; for (int i = ; i<(int)strlen(recvbuf); i++) { recvbuf[i] = '\0'; } while (bytesRecv == SOCKET_ERROR) { CMessageDlg message; message.DoModal(); bytesRecv = recv(sockClient, recvbuf, sizeof(recvbuf), ); }
客户端代码:(数据库操作完成之后)
char buff[] = "op_end"; send(sockClient, buff, sizeof(buff), ); closesocket(sockClient);
注:由于经验不足,本方法可能有很多内存释放,资源利用等细节没有考虑到,所以本方法仅供参考。另外有关socket编程部分的代码参考:
http://blog.csdn.net/chence19871/article/details/44019633