Make WordPress Core

Opened 16 years ago

Closed 15 years ago

#2233 closed enhancement (invalid)

wpdb constructor should create a new connection

Reported by: thomasq Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.0
Component: Administration Keywords:
Focuses: Cc:


The constructor for MySQL connections should create a new transport connection each time
it is called. Otherwise, an import plugin that wants to connect on a database that resides
on the same server as the Wordpress database may incorrectly mess up the Wordpress $wpdb.

Attachments (2)

d (651 bytes) - added by thomasq 16 years ago.
add_link_identifiers.patch (897 bytes) - added by abhay 16 years ago.

Download all attachments as: .zip

Change History (11)

16 years ago

  • Attachment d added

#1 @davidhouse
16 years ago

  • Keywords bg|dev-feedback added
  • Milestone set to 2.1
  • Severity changed from normal to enhancement

Would this break anything? I can't see WordPress creating another instance of $wpdb, so it should be harmless.

#2 @thomasq
16 years ago

I do not foresee any breakage; on my setup, Wordpress seems to be happy with the change.

#3 @masquerade
16 years ago

A better solution would be using $this->dbh as part of all the mysql calls in the wpdb class instead, so that anyone else who uses mysql calls can use their constructor, as you should, for their own code. This would also allow multiple instances of the wpdb class.

#4 @abhay
16 years ago

I would agree with masquerade on that one. If we want to allow for multiple connections on the database, it makes more sense to include the link identifiers in all calls to the database. This allows for multiple unique instances of the database connection to be available if necessary. I've added a patch that does this. There are only two instances where it matters.

#5 @Libertus
16 years ago

masquerade, abhay,

The original patch from thomasq is correct. wpdb constructor must force a new database connection, otherwise imports from different databases on the same host with the same username and password as the main WP database will fail.

The problem is not where $this->dbh is used. The problem is that $this->dbh on two supposedly independent wpdb objects are references to the same underlying database connection object, making wpdb::select() change the active database for all of them, including the global $wpdb, which side-effect causes the failure.

#6 @masquerade
16 years ago

While partially correct, creating a new object will not do exactly what you think. Both instances of wpdb will use the same database link either way, because $this->dbh is not used in the calls to mysql_*, so either way, both are needed, thomasq's patch is insufficient.

#7 @matt
15 years ago

  • Milestone changed from 2.1 to 2.2

#8 @foolswisdom
15 years ago

  • Keywords dev-feedback added; bg|dev-feedback removed
  • Milestone changed from 2.2 to 2.4

#9 @rob1n
15 years ago

  • Keywords dev-feedback removed
  • Milestone 2.4 deleted
  • Resolution set to invalid
  • Status changed from new to closed

wpdb now uses $this->dbh in all its mysql_* calls.

Note: See TracTickets for help on using tickets.