diff --git a/src/TestSessionController.php b/src/TestSessionController.php
index f8579bd..1f862f5 100644
--- a/src/TestSessionController.php
+++ b/src/TestSessionController.php
@@ -16,6 +16,7 @@ use SilverStripe\Forms\FormAction;
 use SilverStripe\Forms\HiddenField;
 use SilverStripe\Forms\TextField;
 use SilverStripe\ORM\ArrayList;
+use SilverStripe\ORM\Connect\TempDatabase;
 use SilverStripe\ORM\DB;
 use SilverStripe\ORM\FieldType\DBHTMLText;
 use SilverStripe\Security\Permission;
@@ -315,8 +316,9 @@ class TestSessionController extends Controller
 
         $this->extend('onBeforeClear');
 
-        if (SapphireTest::using_temp_db()) {
-            SapphireTest::empty_temp_db();
+        $tempDB = new TempDatabase();
+        if ($tempDB->isUsed()) {
+            $tempDB->clearAllData();
         }
 
         if (isset($_SESSION['_testsession_codeblocks'])) {
@@ -359,7 +361,8 @@ class TestSessionController extends Controller
      */
     public function isTesting()
     {
-        return SapphireTest::using_temp_db();
+        $tempDB = new TempDatabase();
+        return $tempDB->isUsed();
     }
 
     /**
diff --git a/src/TestSessionEnvironment.php b/src/TestSessionEnvironment.php
index ccc395a..fb74c9e 100644
--- a/src/TestSessionEnvironment.php
+++ b/src/TestSessionEnvironment.php
@@ -12,7 +12,7 @@ use SilverStripe\Core\Extensible;
 use SilverStripe\Core\Injector\Injectable;
 use SilverStripe\Core\Injector\Injector;
 use SilverStripe\Dev\FixtureFactory;
-use SilverStripe\Dev\SapphireTest;
+use SilverStripe\ORM\Connect\TempDatabase;
 use SilverStripe\ORM\DatabaseAdmin;
 use SilverStripe\ORM\DB;
 use SilverStripe\ORM\FieldType\DBDatetime;
@@ -195,7 +195,7 @@ class TestSessionEnvironment
         $this->extend('onBeforeApplyState', $state);
 
         // back up source
-        global $databaseConfig;
+        $databaseConfig = DB::getConfig();
         $this->oldDatabaseName = $databaseConfig['database'];
 
         // Load existing state from $this->state into $state, if there is any
@@ -243,7 +243,8 @@ class TestSessionEnvironment
 
             if (!$dbExists) {
                 // Create a new one with a randomized name
-                $dbName = SapphireTest::create_temp_db();
+                $tempDB = new TempDatabase();
+                $dbName = $tempDB->build();
 
                 $state->database = $dbName; // In case it's changed by the call to SapphireTest::create_temp_db();
 
@@ -355,9 +356,12 @@ class TestSessionEnvironment
 
                 $this->applyState($json);
             } catch (Exception $e) {
-                throw new \Exception("A test session appears to be in progress, but we can't retrieve the details. "
-                    . "Try removing the " . $this->getFilePath() . " file. Inner "
-                    . "error: " . $e->getMessage());
+                throw new Exception(
+                    "A test session appears to be in progress, but we can't retrieve the details.\n"
+                    . "Try removing the " . $this->getFilePath() . " file.\n"
+                    . "Inner error: " . $e->getMessage() . "\n"
+                    . "Stacktrace: " . $e->getTraceAsString()
+                );
             }
         }
     }
@@ -389,7 +393,8 @@ class TestSessionEnvironment
     {
         $this->extend('onBeforeEndTestSession');
 
-        if (SapphireTest::using_temp_db()) {
+        $tempDB = new TempDatabase();
+        if ($tempDB->isUsed()) {
             $state = $this->getState();
             $dbConn = DB::get_schema();
             $dbExists = $dbConn->databaseExists($state->database);