You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by "pelle.ullberg" <pe...@gmail.com> on 2013/10/09 14:26:33 UTC
Able to reconnect previously shutdown in-memory derby database
Hi,
Could someone please explain whats wrong with this little unit test that I
mocked up? I'm using derby version 10.10.1.1
Basically I create an in-memory derby database, shut it down and then
expected it to not exist anymore. But if the unit test is right, I can
actually reconnect to it.
Best regards
/Pelle
Code is below!
package com.klarna.derby;
import org.apache.derby.jdbc.EmbeddedDriver;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.UUID;
public class DerbyUtilsTest {
private static final Logger LOGGER =
LoggerFactory.getLogger(DerbyUtilsTest.class);
@Test(expected = SQLException.class)
public void verifyDerbyShutdown() throws SQLException {
String url = "jdbc:derby:memory:" + UUID.randomUUID().toString();
Connection connection = DriverManager.getConnection(url +
";create=true");
// Ping derby just to make sure we got it up and running
connection.prepareCall("select * from
SYS.SYSTABLES").executeQuery();
try {
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException e) {
// This exception is expected:
http://db.apache.org/derby/docs/10.3/devguide/tdevdvlp20349.html
Assert.assertEquals("Derby system shutdown.", e.getMessage());
} finally {
// Make sure old driver is collected
System.gc();
try {
// Re-rgeister driver so that new derby jdbc instances may
be spawned.
DriverManager.registerDriver(new EmbeddedDriver());
} catch (SQLException e) {
LOGGER.error("Failed to re-register Derby embedded driver: "
+ e.getMessage(), e);
}
}
// Expected this to throw something like 'java.sql.SQLException:
Database 'memory:d77d6863-7624-4990-86fb-2e40a5a1e04d' not found'
DriverManager.getConnection(url);
}
}
--
View this message in context: http://apache-database.10148.n7.nabble.com/Able-to-reconnect-previously-shutdown-in-memory-derby-database-tp134573.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.
Re: Able to reconnect previously shutdown in-memory derby database
Posted by Katherine Marsden <km...@sbcglobal.net>.
On 10/9/2013 5:26 AM, pelle.ullberg wrote:
> Hi,
>
> Could someone please explain whats wrong with this little unit test that I
> mocked up? I'm using derby version 10.10.1.1
>
> Basically I create an in-memory derby database, shut it down and then
> expected it to not exist anymore. But if the unit test is right, I can
> actually reconnect to it.
The drop=true attribute will drop the in-memory database.
http://db.apache.org/derby/docs/10.10/ref/rrefattribdrop.html
Re: Able to reconnect previously shutdown in-memory derby database
Posted by Peter Ondruška <pe...@yahoo.com>.
My guess is that similar to filesystem if you only shutdown Derby without JVM exit database is still there. Similar to filesystem where you need to remove database from filesystem.
Peter
> On 9 Oct 2013, at 14:26, "pelle.ullberg" <pe...@gmail.com> wrote:
>
> Hi,
>
> Could someone please explain whats wrong with this little unit test that I
> mocked up? I'm using derby version 10.10.1.1
>
> Basically I create an in-memory derby database, shut it down and then
> expected it to not exist anymore. But if the unit test is right, I can
> actually reconnect to it.
>
> Best regards
> /Pelle
>
> Code is below!
>
>
> package com.klarna.derby;
>
> import org.apache.derby.jdbc.EmbeddedDriver;
> import org.junit.Assert;
> import org.junit.Test;
> import org.slf4j.Logger;
> import org.slf4j.LoggerFactory;
>
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.SQLException;
> import java.util.UUID;
>
> public class DerbyUtilsTest {
>
> private static final Logger LOGGER =
> LoggerFactory.getLogger(DerbyUtilsTest.class);
>
> @Test(expected = SQLException.class)
> public void verifyDerbyShutdown() throws SQLException {
> String url = "jdbc:derby:memory:" + UUID.randomUUID().toString();
>
> Connection connection = DriverManager.getConnection(url +
> ";create=true");
>
> // Ping derby just to make sure we got it up and running
> connection.prepareCall("select * from
> SYS.SYSTABLES").executeQuery();
>
> try {
> DriverManager.getConnection("jdbc:derby:;shutdown=true");
> } catch (SQLException e) {
> // This exception is expected:
> http://db.apache.org/derby/docs/10.3/devguide/tdevdvlp20349.html
> Assert.assertEquals("Derby system shutdown.", e.getMessage());
> } finally {
> // Make sure old driver is collected
> System.gc();
> try {
> // Re-rgeister driver so that new derby jdbc instances may
> be spawned.
> DriverManager.registerDriver(new EmbeddedDriver());
> } catch (SQLException e) {
> LOGGER.error("Failed to re-register Derby embedded driver: "
> + e.getMessage(), e);
> }
> }
>
> // Expected this to throw something like 'java.sql.SQLException:
> Database 'memory:d77d6863-7624-4990-86fb-2e40a5a1e04d' not found'
> DriverManager.getConnection(url);
>
> }
>
> }
>
>
>
>
> --
> View this message in context: http://apache-database.10148.n7.nabble.com/Able-to-reconnect-previously-shutdown-in-memory-derby-database-tp134573.html
> Sent from the Apache Derby Users mailing list archive at Nabble.com.