Question about destructor of database and multiple use access

Suliman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 28 07:01:45 PDT 2016


I have for next queston.

For example I have class for working with DB (I am using ddbc 
driver). I put some variables as class fields, and init them in 
constructor:

class GDB
{
	Statement stmt;
	Config config;
	MySQLDriver driver;
	DataSource ds;

	this(Config config)
	{
		this.config = config;
	 	driver = new MySQLDriver();
	 	string[string] params;
	    string url = MySQLDriver.generateUrl("localhost", 3306, 
"geodb");
	    params = MySQLDriver.setUserAndPassword("root", "123");
	    ds = new ConnectionPoolDataSourceImpl(driver, url, params);
	}
	
	
	void dbInsert(string login, string date, string type, string 
data)
	{
		string sqlinsert = (`INSERT INTO test (userlogin, date, type, 
data) VALUES ('%s', '%s', '%s', '%s') `, login, date, type, data);
		stmt.executeUpdate(sqlinsert);
	}


	void getIMGsMetadataFromDB(Json request)
	{
		 string sqlSelect = "SELECT * FROM test";
		...
                 stmt.executeQuery ...

	}


....
}

I can't understand in which place I should put:

	auto conn = ds.getConnection();
	scope(exit) conn.close();

	auto stmt = conn.createStatement();
	scope(exit) stmt.close();
	
1. Should declaration of them be field of class?
2. Should I call destructor and how it's should like?
3. If I will not call it would it wrong?
4. If 100 users will come to my site, my code will open 100 
connections? And would open every new connection for every 
request? Can I open single connection and use it for all users?




More information about the Digitalmars-d-learn mailing list