public class SSessionJdbc extends SSessionI
SSessionJdbc.open creates a new session and associates it with the current thread.
All transactions must start with a begin()
, which normally creates a new DataSet to store records in. They
with either a commit()
or a rollback()
which normally destroy the DataSet and all records in it.
commit
also flush()
es any outsanding changes.
Methods are then provided to retrieve recrods from the databse by primary key or general query.
There are also convenience method for performing raw SQL queries should that be required.
Modifier | Constructor and Description |
---|---|
protected |
SSessionJdbc()
No direct creation of Connections.
|
Modifier and Type | Method and Description |
---|---|
protected void |
associateWithThread() |
void |
attachToThread()
Re-attacheds a detached connection to the current thread.
|
void |
begin()
Start a new transaction.
|
void |
begin(SDataSet ds)
Begin a new transaction based on a previously detached data set
|
void |
close()
Main method for closing a session.
|
void |
commit()
Flush and purge the transaction cache to the database, and then commit the transaction.
|
SDataSet |
commitAndDetachDataSet() |
<RI extends SRecordInstance> |
create(SRecordMeta<RI> rmeta,
java.lang.Object... keys)
Create a new record.
|
<RI extends SRecordInstance> |
createWithGeneratedKey(SRecordMeta<RI> rmeta)
Creates a new object with a newly generated key.
|
SSessionJdbc |
detachFromThread()
Enables the SimpleORM connection to be disassociated with the current thread, and then possibly attached to
another thread.
|
void |
dirtyPurge(SRecordInstance rinst)
Removes this record from the cache but without flushing it.
|
protected void |
dissassociateFromThread() |
<RI extends SRecordInstance> |
find(SRecordMeta<RI> rmeta,
java.lang.Object... keys) |
<RI extends SRecordInstance> |
find(SRecordMeta<RI> rmeta,
SQueryMode queryMode,
java.lang.Object... keys)
Same as findOrCreate but if not existing returns null rather than creating a new record.
|
<RI extends SRecordInstance> |
findOrCreate(SRecordMeta<RI> rmeta,
java.lang.Object... keys) |
<RI extends SRecordInstance> |
findOrCreate(SRecordMeta<RI> rmeta,
SFieldScalar[] selectList,
SQueryMode queryMode,
java.lang.Object... keys)
Find a record or create one if it does not already exist.
|
<RI extends SRecordInstance> |
findOrCreate(SRecordMeta<RI> rmeta,
SQueryMode queryMode,
java.lang.Object... keys) |
<RI extends SRecordInstance> |
findOrCreate(SRecordMeta<RI> rmeta,
SSelectMode selectMode,
java.lang.Object... keys) |
<RI extends SRecordInstance> |
findOrCreate(SRecordMeta<RI> rmeta,
SSelectMode selectMode,
SQueryMode queryMode,
java.lang.Object... keys) |
void |
flush()
Flush all records of tables in this transaction to the database.
|
void |
flush(SRecordInstance instance)
Flush this instance to the database.
|
void |
flushAndPurge()
Flushes and Purges all record instances.
|
void |
flushAndPurge(SRecordInstance ri)
Flushes this record instance to the database, removes it from the transaction cache, and then destroys the record
so that it can no longer be used.
|
SDataSet |
getDataSet() |
javax.sql.DataSource |
getDataSource() |
SDriver |
getDriver()
Eg.
|
java.sql.Connection |
getJdbcConnection() |
SLog |
getLogger() |
SStatistics |
getStatistics() |
static SSessionJdbc |
getThreadLocalSession()
Retrieve or create the SSession associated with this "Context".
|
boolean |
hasBegun()
Transaction is active between begin and commit.
|
protected void |
innerOpen(javax.sql.DataSource source,
java.lang.String connectionName,
SDriver driver)
Allows SSession to be subclassed.
|
<RI extends SRecordInstance> |
mustFind(SRecordMeta<RI> rmeta,
java.lang.Object... keys) |
<RI extends SRecordInstance> |
mustFind(SRecordMeta<RI> rmeta,
SQueryMode queryMode,
java.lang.Object... keys) |
static SSessionJdbc |
open(javax.sql.DataSource source,
java.lang.String connectionName) |
static SSessionJdbc |
open(javax.sql.DataSource source,
java.lang.String connectionName,
SDriver driver)
Attaches a SimpleORM connection to the current thread based on conf.
|
<RI extends SRecordInstance> |
query(SQuery<RI> qry)
Execute the query and return a list of records.
|
<RI extends SRecordInstance> |
queryNoFlush(SQuery<RI> qry)
Query, but without first flushing the cache, not normally used.
|
<RI extends SRecordInstance> |
queryOnlyRecord(SQuery<RI> qry)
Same as query but throws an exception if there is more than one record returned.
|
java.util.Map |
rawQueryMap(java.lang.String sql,
boolean flush,
java.lang.Object... params)
Returns at most one row, else exception.
|
java.util.List<java.util.Map> |
rawQueryMaps(java.lang.String sql,
boolean flush,
java.lang.Object... params)
Executes sql with parameters, returns a list of Maps of values, one map per row.
|
java.util.Map |
rawQueryOneMap(java.lang.String sql,
boolean flush,
java.lang.Object... params)
Returns exactly one row, else exception.
|
java.lang.Object |
rawQuerySingle(java.lang.String sql,
boolean flush,
java.lang.Object... params)
Returns a single value of at most one row.
|
int |
rawUpdateDB(java.lang.String sql,
java.lang.Object... params)
Convenience routine for doing bulk updates using raw JDBC.
|
int |
rawUpdateDBNoFlush(java.lang.String sql,
java.lang.Object... params) |
void |
rollback()
Purges the cache and rolls back the transaction.
|
void |
setTransactionIsolation(int mode)
Set the transaction isolation mode.
|
java.lang.String |
toString() |
public static SSessionJdbc open(javax.sql.DataSource source, java.lang.String connectionName, SDriver driver)
connectionName is a simple name that can be used to identify this connection in log traces etc. Eg. the session ID plus the user name. But keep it short for logging.
The driver is usually determined automatically (if null) based on the conf, but if it is overriden make sure to create a new driver instance for each connection.
See TestUte.java for an example of how to create a trivial DataSource from an old style DriverManager
public static SSessionJdbc open(javax.sql.DataSource source, java.lang.String connectionName)
protected void innerOpen(javax.sql.DataSource source, java.lang.String connectionName, SDriver driver)
We use DataSource rather than just Connection to allow a driver to create a second connection to generate keys etc. But that has never been used, and some old code that potentially supported it has been removed.
protected void associateWithThread()
protected void dissassociateFromThread()
public static SSessionJdbc getThreadLocalSession()
(In extras there is an unsupported EJB.SConnectionEJB class which dispached to getTransactionConnection instead.)
It may or may not be begun. May be used to set properties for the connection, which persist between transactions but not between attach ments.
This is never used within SimpleOrm itself, it is just a convenience method for users.
public void close()
Closes the JDBC connection and then calls detachWithoutClosing
to detach the SimpleORM connection
from the current thread. Should usually be put in a finally clause. No error if already detached or closed so
safe in finally clauses.
public SSessionJdbc detachFromThread()
Rarely used, normally better to just create a session in the appropriate thread. Both Swing and EJBs can be used without doing this.
Do not not do it unless you really have to.
public void attachToThread()
detachFromThread()
public javax.sql.DataSource getDataSource()
public void begin()
public void begin(SDataSet ds)
public boolean hasBegun()
public void commit()
begin()
must be used to start the next transaction.
Also closes any open SResultSets and SPreparedStatements
public SDataSet commitAndDetachDataSet()
public void rollback()
Also closes any open SResultSets and SPreparedStatements
public void setTransactionIsolation(int mode)
mode
- as defined by java.sql.Connection.setTransactionIsolationpublic void flush()
public void flush(SRecordInstance instance)
SSession.flush()
in reponse to
commit
but can also be called expicitly if the update order needs to be modified. This method does
nothing unless the record is dirty.
SSession#flush
public void flushAndPurge(SRecordInstance ri)
findOrCreate
will requery the data base and produce a
new record.
This is useful if one wants to do raw JDBC updates on the record, and be guaranteed not to have an inconsistent cache. (Where bulk updates can be used they are several times faster than updates made via the JVM -- see the benchmarks section in the white paper.)
It can also be useful to force a requery of records if optmistic locks are being broken, although care must be taken not to rely on values returned by a outdated query.
SSession#flush
public void flushAndPurge()
SRecordInstance#flushAndPurge
,
SRecordMeta#flushAndPurge
public java.lang.String toString()
toString
in class java.lang.Object
public <RI extends SRecordInstance> java.util.List<RI> queryNoFlush(SQuery<RI> qry)
query
public <RI extends SRecordInstance> java.util.List<RI> query(SQuery<RI> qry)
public <RI extends SRecordInstance> RI queryOnlyRecord(SQuery<RI> qry)
public <RI extends SRecordInstance> RI findOrCreate(SRecordMeta<RI> rmeta, SFieldScalar[] selectList, SQueryMode queryMode, java.lang.Object... keys)
findOrCreate
in class SSessionI
public <RI extends SRecordInstance> RI findOrCreate(SRecordMeta<RI> rmeta, SSelectMode selectMode, SQueryMode queryMode, java.lang.Object... keys)
public <RI extends SRecordInstance> RI findOrCreate(SRecordMeta<RI> rmeta, SSelectMode selectMode, java.lang.Object... keys)
public <RI extends SRecordInstance> RI findOrCreate(SRecordMeta<RI> rmeta, SQueryMode queryMode, java.lang.Object... keys)
public <RI extends SRecordInstance> RI findOrCreate(SRecordMeta<RI> rmeta, java.lang.Object... keys)
public <RI extends SRecordInstance> RI mustFind(SRecordMeta<RI> rmeta, SQueryMode queryMode, java.lang.Object... keys)
public <RI extends SRecordInstance> RI mustFind(SRecordMeta<RI> rmeta, java.lang.Object... keys)
public <RI extends SRecordInstance> RI find(SRecordMeta<RI> rmeta, SQueryMode queryMode, java.lang.Object... keys)
public <RI extends SRecordInstance> RI find(SRecordMeta<RI> rmeta, java.lang.Object... keys)
public <RI extends SRecordInstance> RI create(SRecordMeta<RI> rmeta, java.lang.Object... keys)
Assumes the record is new, will do an insert at flush time which will cause a unique index violation if it was not. Use a findOrCreate followed by assertNewRow if it is unclear whether the row is really new.
public <RI extends SRecordInstance> RI createWithGeneratedKey(SRecordMeta<RI> rmeta)
SGENERATED_KEY
.
Always creates a new empty record. ## This code is partially copied into SRecordInstance.attach() for records created while detached.
public void dirtyPurge(SRecordInstance rinst)
Dangerous, use with care.
public int rawUpdateDB(java.lang.String sql, java.lang.Object... params)
Flushes before performing update. But does not requery any updated records.
public int rawUpdateDBNoFlush(java.lang.String sql, java.lang.Object... params)
public java.util.List<java.util.Map> rawQueryMaps(java.lang.String sql, boolean flush, java.lang.Object... params)
SELECT MAX(SALARY) AS MAX_SAL ... FROM ....
public java.util.Map rawQueryMap(java.lang.String sql, boolean flush, java.lang.Object... params)
public java.util.Map rawQueryOneMap(java.lang.String sql, boolean flush, java.lang.Object... params)
public java.lang.Object rawQuerySingle(java.lang.String sql, boolean flush, java.lang.Object... params)
public SLog getLogger()
public java.sql.Connection getJdbcConnection()
public SDataSet getDataSet()
public SDriver getDriver()
public SStatistics getStatistics()