Skip to content
This repository

Added JDatabaseDriver::disconnect() to support disconnecting from the database cleanly. #1189

Merged
merged 1 commit into from almost 2 years ago

3 participants

Rob Schley Matias Griese Sam Moffatt
Rob Schley

This is useful when building applications with JApplicationDaemon because the database resource that gets copied after forking is shared between parent and child and if either of the processes close the connect, it is closed for both. With this, you can use the onFork event to disconnect and reconnect the database to get a new, unshared resource.

Matias Griese
mahagr commented

There's a related feature I've really would like to see in Joomla platform: reconnect/retry if database query fails because of the connection was cut by the server timeout. The most common reason is JInstaller extracting a large zip and trying to insert component without checking if the connection is still there.

Sam Moffatt pasamio merged commit 1ef2ad6 into from
Sam Moffatt pasamio closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 05, 2012
Rob Schley Added JDatabaseDriver::disconnect() to support disconnecting from the
database cleanly.
836bb46
This page is out of date. Refresh to see the latest.
9  libraries/joomla/database/driver.php
@@ -397,6 +397,15 @@ public function __construct($options)
397 397
 	abstract public function connected();
398 398
 
399 399
 	/**
  400
+	 * Disconnects the database.
  401
+	 *
  402
+	 * @return  void
  403
+	 *
  404
+	 * @since   12.1
  405
+	 */
  406
+	abstract public function disconnect();
  407
+
  408
+	/**
400 409
 	 * Drops a table from the database.
401 410
 	 *
402 411
 	 * @param   string   $table     The name of the database table to drop.
29  libraries/joomla/database/driver/mysql.php
@@ -74,6 +74,19 @@ public function __construct($options)
74 74
 	}
75 75
 
76 76
 	/**
  77
+	 * Destructor.
  78
+	 *
  79
+	 * @since   12.1
  80
+	 */
  81
+	public function __destruct()
  82
+	{
  83
+		if (is_resource($this->connection))
  84
+		{
  85
+			mysql_close($this->connection);
  86
+		}
  87
+	}
  88
+
  89
+	/**
77 90
 	 * Connects to the database if needed.
78 91
 	 *
79 92
 	 * @return  void  Returns void if the database connected successfully.
@@ -114,16 +127,18 @@ public function connect()
114 127
 	}
115 128
 
116 129
 	/**
117  
-	 * Destructor.
  130
+	 * Disconnects the database.
  131
+	 *
  132
+	 * @return  void
118 133
 	 *
119 134
 	 * @since   12.1
120 135
 	 */
121  
-	public function __destruct()
  136
+	public function disconnect()
122 137
 	{
123  
-		if (is_resource($this->connection))
124  
-		{
125  
-			mysql_close($this->connection);
126  
-		}
  138
+		// Close the connection.
  139
+		mysql_close($this->connection);
  140
+
  141
+		$this->connection = null;
127 142
 	}
128 143
 
129 144
 	/**
@@ -173,7 +188,7 @@ public function connected()
173 188
 	{
174 189
 		if (is_resource($this->connection))
175 190
 		{
176  
-			return mysql_ping($this->connection);
  191
+			return @mysql_ping($this->connection);
177 192
 		}
178 193
 
179 194
 		return false;
22  libraries/joomla/database/driver/mysqli.php
@@ -50,6 +50,19 @@ public function __construct($options)
50 50
 	}
51 51
 
52 52
 	/**
  53
+	 * Destructor.
  54
+	 *
  55
+	 * @since   12.1
  56
+	 */
  57
+	public function __destruct()
  58
+	{
  59
+		if (is_callable($this->connection, 'close'))
  60
+		{
  61
+			mysqli_close($this->connection);
  62
+		}
  63
+	}
  64
+
  65
+	/**
53 66
 	 * Connects to the database if needed.
54 67
 	 *
55 68
 	 * @return  void  Returns void if the database connected successfully.
@@ -121,16 +134,21 @@ public function connect()
121 134
 	}
122 135
 
123 136
 	/**
124  
-	 * Destructor.
  137
+	 * Disconnects the database.
  138
+	 *
  139
+	 * @return  void
125 140
 	 *
126 141
 	 * @since   12.1
127 142
 	 */
128  
-	public function __destruct()
  143
+	public function disconnect()
129 144
 	{
  145
+		// Close the connection.
130 146
 		if (is_callable($this->connection, 'close'))
131 147
 		{
132 148
 			mysqli_close($this->connection);
133 149
 		}
  150
+
  151
+		$this->connection = null;
134 152
 	}
135 153
 
136 154
 	/**
18  libraries/joomla/database/driver/oracle.php
@@ -64,6 +64,17 @@ public function __construct($options)
64 64
 	}
65 65
 
66 66
 	/**
  67
+	 * Destructor.
  68
+	 *
  69
+	 * @since   12.1
  70
+	 */
  71
+	public function __destruct()
  72
+	{
  73
+		$this->freeResult();
  74
+		unset($this->connection);
  75
+	}
  76
+
  77
+	/**
67 78
 	 * Connects to the database if needed.
68 79
 	 *
69 80
 	 * @return  void  Returns void if the database connected successfully.
@@ -84,12 +95,15 @@ public function connect()
84 95
 	}
85 96
 
86 97
 	/**
87  
-	 * Destructor.
  98
+	 * Disconnects the database.
  99
+	 *
  100
+	 * @return  void
88 101
 	 *
89 102
 	 * @since   12.1
90 103
 	 */
91  
-	public function __destruct()
  104
+	public function disconnect()
92 105
 	{
  106
+		// Close the connection.
93 107
 		$this->freeResult();
94 108
 		unset($this->connection);
95 109
 	}
17  libraries/joomla/database/driver/pdo.php
@@ -84,6 +84,17 @@ public function __construct($options)
84 84
 	}
85 85
 
86 86
 	/**
  87
+	 * Destructor.
  88
+	 *
  89
+	 * @since   12.1
  90
+	 */
  91
+	public function __destruct()
  92
+	{
  93
+		$this->freeResult();
  94
+		unset($this->connection);
  95
+	}
  96
+
  97
+	/**
87 98
 	 * Connects to the database if needed.
88 99
 	 *
89 100
 	 * @return  void  Returns void if the database connected successfully.
@@ -290,11 +301,13 @@ public function connect()
290 301
 	}
291 302
 
292 303
 	/**
293  
-	 * Destructor.
  304
+	 * Disconnects the database.
  305
+	 *
  306
+	 * @return  void
294 307
 	 *
295 308
 	 * @since   12.1
296 309
 	 */
297  
-	public function __destruct()
  310
+	public function disconnect()
298 311
 	{
299 312
 		$this->freeResult();
300 313
 		unset($this->connection);
24  libraries/joomla/database/driver/postgresql.php
@@ -78,6 +78,19 @@ public function __construct( $options )
78 78
 	}
79 79
 
80 80
 	/**
  81
+	 * Database object destructor
  82
+	 *
  83
+	 * @since 12.1
  84
+	 */
  85
+	public function __destruct()
  86
+	{
  87
+		if (is_resource($this->connection))
  88
+		{
  89
+			pg_close($this->connection);
  90
+		}
  91
+	}
  92
+
  93
+	/**
81 94
 	 * Connects to the database if needed.
82 95
 	 *
83 96
 	 * @return  void  Returns void if the database connected successfully.
@@ -112,16 +125,21 @@ public function connect()
112 125
 	}
113 126
 
114 127
 	/**
115  
-	 * Database object destructor
  128
+	 * Disconnects the database.
116 129
 	 *
117  
-	 * @since 12.1
  130
+	 * @return  void
  131
+	 *
  132
+	 * @since   12.1
118 133
 	 */
119  
-	public function __destruct()
  134
+	public function disconnect()
120 135
 	{
  136
+		// Close the connection.
121 137
 		if (is_resource($this->connection))
122 138
 		{
123 139
 			pg_close($this->connection);
124 140
 		}
  141
+
  142
+		$this->connection = null;
125 143
 	}
126 144
 
127 145
 	/**
13  libraries/joomla/database/driver/sqlite.php
@@ -63,6 +63,19 @@ public function __destruct()
63 63
 	}
64 64
 
65 65
 	/**
  66
+	 * Disconnects the database.
  67
+	 *
  68
+	 * @return  void
  69
+	 *
  70
+	 * @since   12.1
  71
+	 */
  72
+	public function disconnect()
  73
+	{
  74
+		$this->freeResult();
  75
+		unset($this->connection);
  76
+	}
  77
+
  78
+	/**
66 79
 	 * Drops a table from the database.
67 80
 	 *
68 81
 	 * @param   string   $tableName  The name of the database table to drop.
22  libraries/joomla/database/driver/sqlsrv.php
@@ -86,6 +86,19 @@ public function __construct($options)
86 86
 	}
87 87
 
88 88
 	/**
  89
+	 * Destructor.
  90
+	 *
  91
+	 * @since   12.1
  92
+	 */
  93
+	public function __destruct()
  94
+	{
  95
+		if (is_resource($this->connection))
  96
+		{
  97
+			sqlsrv_close($this->connection);
  98
+		}
  99
+	}
  100
+
  101
+	/**
89 102
 	 * Connects to the database if needed.
90 103
 	 *
91 104
 	 * @return  void  Returns void if the database connected successfully.
@@ -131,16 +144,21 @@ public function connect()
131 144
 	}
132 145
 
133 146
 	/**
134  
-	 * Destructor.
  147
+	 * Disconnects the database.
  148
+	 *
  149
+	 * @return  void
135 150
 	 *
136 151
 	 * @since   12.1
137 152
 	 */
138  
-	public function __destruct()
  153
+	public function disconnect()
139 154
 	{
  155
+		// Close the connection.
140 156
 		if (is_resource($this->connection))
141 157
 		{
142 158
 			sqlsrv_close($this->connection);
143 159
 		}
  160
+
  161
+		$this->connection = null;
144 162
 	}
145 163
 
146 164
 	/**
1  tests/core/mock/database/driver.php
@@ -37,6 +37,7 @@ public static function create($test, $nullDate = '0000-00-00 00:00:00', $dateFor
37 37
 		$methods = array(
38 38
 			'connect',
39 39
 			'connected',
  40
+			'disconnect',
40 41
 			'dropTable',
41 42
 			'escape',
42 43
 			'execute',
22  tests/suites/unit/joomla/database/stubs/nosqldriver.php
@@ -63,29 +63,29 @@ public function connect()
63 63
 		return true;
64 64
 	}
65 65
 
66  
-
67 66
 	/**
68  
-
69 67
 	 * Determines if the connection to the server is active.
70  
-
71 68
 	 *
72  
-
73 69
 	 * @return  boolean  True if connected to the database engine.
74  
-
75 70
 	 *
76  
-
77 71
 	 * @since   11.4
78  
-
79 72
 	 */
80  
-
81 73
 	public function connected()
82  
-
83 74
 	{
84  
-
85 75
 		return true;
86  
-
87 76
 	}
88 77
 
  78
+	/**
  79
+	 * Disconnects the database.
  80
+	 *
  81
+	 * @return  void
  82
+	 *
  83
+	 * @since   12.1
  84
+	 */
  85
+	public function disconnect()
  86
+	{
  87
+		return;
  88
+	}
89 89
 
90 90
 	/**
91 91
 	 * Drops a table from the database.
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.