Skip to content

Commit

Permalink
Added BEGIN/COMMIT/ROLLBACK
Browse files Browse the repository at this point in the history
  • Loading branch information
systay committed May 17, 2012
1 parent b71c1b3 commit 6d5a44f
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 0 deletions.
4 changes: 4 additions & 0 deletions shell/CHANGES.txt
@@ -1,3 +1,7 @@
1.8.M03
--------------------
o Added BEGIN TRANSACTION/COMMIT/ROLLBACK

1.8.M02 (2012-05-11)
--------------------
o Shell can now be exited with Ctrl-D
Expand Down
11 changes: 11 additions & 0 deletions shell/src/docs/dev/shell.txt
Expand Up @@ -247,6 +247,17 @@ Example: +index -i persons name+ (will index the name for the current node or re
* +-i+ will index a key-value pair in an index for the current node/relationship. If no value is given the property value for that key for the current node is used as value.
* +-r+ will remove a key-value pair (if it exists) from an index for the current node/relationship. Key and value is optional.

=== Transactions ===
It is useful to be able to test changes, and then being able to commit or rollback said changes.

Transactions can be nested. With a nested transaction, a commit does not write any changes to disk, except for the
top level transaction. A rollback, however works regardless of the level of the transaction. It will roll back
all open transactions.

* +begin transaction+ Starts a transaction.
* +commit+ Commits a transaction.
* +rollback+ Rollbacks all open transactions.

[[shell-extending]]
== Extending the shell: Adding your own commands ==

Expand Down
84 changes: 84 additions & 0 deletions shell/src/main/java/org/neo4j/shell/kernel/apps/Begin.java
@@ -0,0 +1,84 @@
/**
* Copyright (c) 2002-2012 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.shell.kernel.apps;

import java.rmi.RemoteException;

import javax.transaction.SystemException;
import javax.transaction.Transaction;

import org.neo4j.helpers.Service;
import org.neo4j.kernel.AbstractGraphDatabase;
import org.neo4j.shell.App;
import org.neo4j.shell.AppCommandParser;
import org.neo4j.shell.Output;
import org.neo4j.shell.Session;
import org.neo4j.shell.ShellException;
import org.neo4j.shell.kernel.GraphDatabaseShellServer;

@Service.Implementation(App.class)
public class Begin extends ReadOnlyGraphDatabaseApp
{
@Override
public String getDescription()
{
return "Opens a transaction";
}

@Override
protected String exec( AppCommandParser parser, Session session, Output out )
throws ShellException, RemoteException
{
Transaction tx = currentTransaction( getServer() );

getServer().getDb().beginTx();
Integer txCount = (Integer) session.get( "tx_count" );

int count;
if ( txCount == null )
{
if ( tx == null )
{
count = 0;
} else
{
count = 1;
}
} else
{
count = txCount;
}

session.set( "tx_count", ++count );
out.println("Transaction started");
return null;
}

public static Transaction currentTransaction( GraphDatabaseShellServer server ) throws ShellException
{
try
{
return ((AbstractGraphDatabase) server.getDb()).getTxManager().getTransaction();
} catch ( SystemException e )
{
throw new ShellException( e.getMessage() );
}
}
}
93 changes: 93 additions & 0 deletions shell/src/main/java/org/neo4j/shell/kernel/apps/Commit.java
@@ -0,0 +1,93 @@
/**
* Copyright (c) 2002-2012 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.shell.kernel.apps;

import java.rmi.RemoteException;

import javax.transaction.SystemException;
import javax.transaction.Transaction;

import org.neo4j.helpers.Service;
import org.neo4j.kernel.AbstractGraphDatabase;
import org.neo4j.shell.App;
import org.neo4j.shell.AppCommandParser;
import org.neo4j.shell.Output;
import org.neo4j.shell.Session;
import org.neo4j.shell.ShellException;

@Service.Implementation(App.class)
public class Commit extends ReadOnlyGraphDatabaseApp
{
public static final String tx_count = "tx_count";

@Override
public String getDescription()
{
return "Commits a transaction";
}

@Override
protected String exec( AppCommandParser parser, Session session, Output out )
throws ShellException, RemoteException
{
Integer txCount = (Integer) session.get( tx_count );

if ( txCount == null || txCount.equals( 0 ) )
{
throw new ShellException( "Not in a transaction" );
} else if ( txCount.equals( 1 ) )
{
Transaction tx;
try
{
tx = ((AbstractGraphDatabase) getServer().getDb()).getTxManager().getTransaction();
if ( tx == null )
{
throw fail( session, "Not in a transaction" );
}
} catch ( SystemException e )
{
throw fail( session, "Not in a transaction" );
}

try
{
tx.commit();
session.remove( tx_count );
out.println("Transaction committed");
return null;
} catch ( Exception e )
{
throw fail( session, e.getMessage() );
}
} else
{
session.set( tx_count, --txCount );
out.println("Transaction committed");
return null;
}
}

public static ShellException fail( Session session, String message ) throws ShellException
{
session.remove( tx_count );
return new ShellException( message );
}
}
64 changes: 64 additions & 0 deletions shell/src/main/java/org/neo4j/shell/kernel/apps/Rollback.java
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2002-2012 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.shell.kernel.apps;

import java.rmi.RemoteException;

import javax.transaction.SystemException;
import javax.transaction.Transaction;

import org.neo4j.helpers.Service;
import org.neo4j.shell.App;
import org.neo4j.shell.AppCommandParser;
import org.neo4j.shell.Output;
import org.neo4j.shell.Session;
import org.neo4j.shell.ShellException;

@Service.Implementation(App.class)
public class Rollback extends ReadOnlyGraphDatabaseApp
{
@Override
public String getDescription()
{
return "Rolls back all open transactions";
}

@Override
protected String exec( AppCommandParser parser, Session session, Output out )
throws ShellException, RemoteException
{
Transaction tx = Begin.currentTransaction( getServer() );
if ( tx == null )
{
throw Commit.fail( session, "Not in a transaction" );
} else
{
try
{
tx.rollback();
out.println("Transaction rolled back");
return null;
} catch ( SystemException e )
{
throw new ShellException( e.getMessage() );
}
}
}
}
Expand Up @@ -3,7 +3,9 @@ org.neo4j.shell.apps.Env
org.neo4j.shell.apps.Export
org.neo4j.shell.apps.Help
org.neo4j.shell.apps.Man
org.neo4j.shell.kernel.apps.Begin
org.neo4j.shell.kernel.apps.Cd
org.neo4j.shell.kernel.apps.Commit
org.neo4j.shell.kernel.apps.Create
org.neo4j.shell.kernel.apps.Cypher
org.neo4j.shell.kernel.apps.Ls
Expand All @@ -15,6 +17,7 @@ org.neo4j.shell.kernel.apps.Start
org.neo4j.shell.kernel.apps.Rm
org.neo4j.shell.kernel.apps.Rmrel
org.neo4j.shell.kernel.apps.Rmnode
org.neo4j.shell.kernel.apps.Rollback
org.neo4j.shell.kernel.apps.Set
org.neo4j.shell.kernel.apps.Trav
org.neo4j.shell.kernel.apps.Index
Expand Down

0 comments on commit 6d5a44f

Please sign in to comment.