Returns the current transaction for this thread, or throws an exception if there is no current
transaction. The current transaction is defined as the result of the most recent, same-thread
invocation of beginTransaction() that has not been committed or rolled back.
Use this method for when you expect there to be a current transaction and consider it an
error if there isn't.
Returns the current transaction for this thread, or returns the parameter if there is no
current transaction. You can use null or provide your own object to represent null. See
#getCurrentTransaction() for a definition of "current transaction."
Use this method when you're not sure if there is a current transaction.
The current transaction, or the parameter that was passed in if there is no current
transaction.
prepare(Query query)
publicabstractPreparedQueryprepare(Queryquery)
Prepares a query for execution.
This method returns a PreparedQuery which can be used to execute and retrieve
results from the datastore for query.
This operation will not execute in a transaction even if there is a current transaction and
the provided query is an ancestor query. This operation also ignores the ImplicitTransactionManagementPolicy. If you are preparing an ancestory query and you want it
to execute in a transaction, use #prepare(Transaction, Query).
Exhibits the same behavior as #prepare(Query), but executes within the provided
transaction. It is up to the caller to commit or rollback. Transaction can be null.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eBaseDatastoreService\u003c/code\u003e interface provides common methods for both \u003ccode\u003eDatastoreService\u003c/code\u003e and \u003ccode\u003eAsyncDatastoreService\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetActiveTransactions()\u003c/code\u003e retrieves all uncommitted or unrolled-back transactions initiated by the current thread.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetCurrentTransaction()\u003c/code\u003e returns the most recently started transaction in the current thread, and will throw an exception if there is no current transaction.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetCurrentTransaction(Transaction returnedIfNoTxn)\u003c/code\u003e can be used in place of \u003ccode\u003egetCurrentTransaction()\u003c/code\u003e when the user is unsure whether there is an existing transaction.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eprepare(Query query)\u003c/code\u003e prepares a query for execution outside of a transaction while \u003ccode\u003eprepare(Transaction txn, Query query)\u003c/code\u003e is used when the user wants the execution of the query to be within a given transaction.\u003c/p\u003e\n"]]],[],null,["# Interface BaseDatastoreService (2.0.0)\n\n public interface BaseDatastoreService\n\nMethods that are common between [DatastoreService](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.DatastoreService) and [AsyncDatastoreService](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.AsyncDatastoreService).\n\nMethods\n-------\n\n### getActiveTransactions()\n\n public abstract Collection\u003cTransaction\u003e getActiveTransactions()\n\nReturns all [Transaction](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Transaction)s started by this thread upon which no attempt to commit or\nrollback has been made.\n\n### getCurrentTransaction()\n\n public abstract Transaction getCurrentTransaction()\n\nReturns the current transaction for this thread, or throws an exception if there is no current\ntransaction. The current transaction is defined as the result of the most recent, same-thread\ninvocation of beginTransaction() that has not been committed or rolled back.\n\nUse this method for when you expect there to be a current transaction and consider it an\nerror if there isn't.\n\n### getCurrentTransaction(Transaction returnedIfNoTxn)\n\n public abstract Transaction getCurrentTransaction(Transaction returnedIfNoTxn)\n\nReturns the current transaction for this thread, or returns the parameter if there is no\ncurrent transaction. You can use `null` or provide your own object to represent null. See\n[#getCurrentTransaction()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.BaseDatastoreService#com_google_appengine_api_datastore_BaseDatastoreService_getCurrentTransaction__) for a definition of \"current transaction.\"\n\nUse this method when you're not sure if there is a current transaction.\n\n### prepare(Query query)\n\n public abstract PreparedQuery prepare(Query query)\n\nPrepares a query for execution.\n\nThis method returns a [PreparedQuery](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.PreparedQuery) which can be used to execute and retrieve\nresults from the datastore for `query`.\n\nThis operation will not execute in a transaction even if there is a current transaction and\nthe provided query is an ancestor query. This operation also ignores the [ImplicitTransactionManagementPolicy](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.ImplicitTransactionManagementPolicy). If you are preparing an ancestory query and you want it\nto execute in a transaction, use [#prepare(Transaction, Query)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.BaseDatastoreService#com_google_appengine_api_datastore_BaseDatastoreService_prepare_com_google_appengine_api_datastore_Transaction_com_google_appengine_api_datastore_Query_).\n\n### prepare(Transaction txn, Query query)\n\n public abstract PreparedQuery prepare(Transaction txn, Query query)\n\nExhibits the same behavior as [#prepare(Query)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.BaseDatastoreService#com_google_appengine_api_datastore_BaseDatastoreService_prepare_com_google_appengine_api_datastore_Query_), but executes within the provided\ntransaction. It is up to the caller to commit or rollback. Transaction can be null."]]