public class PersistenceManager
extends java.lang.Object
PersistenceManager
stores and retrieves Java objects to and from a
relational database. Any object that implements interface Record
can be
read from or written to a database using this class.
In addition, any other class can be used directly as PersistenceManager
wraps such other objects into GenericRecord
objects implicitly.
Major aim of class PersistenceManager
is to encapsulate the management
of database related objects such as Connection
, Statement
and
ResultSet
to make sure proper opening and closing especially when used in
conjunction with a connection pool.
In addition PersistenceManager
provides methods that allow for an
abundance of database read/write operations for convenience providing a base
class to extend for individual needs.
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
dburl
reference to JDBC url to use for connections in development mode
|
protected javax.sql.DataSource |
ds
reference to data source to use for connections in production mode
|
static java.lang.String |
NULL_STR |
Constructor and Description |
---|
PersistenceManager()
Create a new object of class PersistenceManager
|
Modifier and Type | Method and Description |
---|---|
java.sql.PreparedStatement |
buildQuery(java.sql.Connection c,
java.lang.String sql,
java.lang.Object[] params)
Create an SQL statement from an SQL string and an array of parameters
|
void |
closeConnectionFinally(java.sql.Connection c)
close a given connection
|
void |
closeResultSetFinally(java.sql.ResultSet rs)
Close a given result set
|
void |
closeStatementFinally(java.sql.Statement s)
Close a given statement
|
void |
commit(java.sql.Connection c) |
java.lang.Object |
delete(java.sql.Connection c,
java.lang.Object o)
Delete a given object from the database
Use this method for single deletes.
|
java.lang.Object |
delete(java.sql.Connection c,
java.lang.Object o,
Record record)
Delete a given object from the database
|
java.lang.Object |
delete(java.lang.Object o)
Delete a given object from the database
Use this method for single deletes.
|
java.lang.Object |
delete(java.lang.Object o,
Record record)
Delete a given object from the database
|
int |
execute(java.sql.Connection c,
java.lang.String sql)
Execute an SQL statement
|
int |
execute(java.sql.Connection c,
java.lang.String sql,
java.lang.Object... params)
Execute an SQL statement
|
int |
execute(java.lang.String sql)
Execute an SQL statement
|
int |
execute(java.lang.String sql,
java.lang.Object... params)
Execute an SQL statement
|
int[] |
executeScript(java.sql.Connection c,
java.lang.String sqlScript)
Execute an SQL script
|
int[] |
executeScript(java.lang.String sqlScript)
Execute an SQL script
|
java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
executeWithKeys(java.sql.Connection c,
java.lang.String sql)
Execute an SQL statement and return keys generated in the database
|
java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
executeWithKeys(java.lang.String sql)
Execute an SQL statement and return keys generated in the database
|
java.sql.Connection |
getConnection()
Get a database connection
|
java.lang.Object |
insert(java.sql.Connection c,
java.lang.Object o)
Add an object to the database
Use this method for single inserts.
|
java.lang.Object |
insert(java.sql.Connection c,
java.lang.Object o,
Record record)
Add an object to the database
|
java.lang.Object |
insert(java.lang.Object o)
Add an object to the database
Use this method for single inserts.
|
java.lang.Object |
insert(java.lang.Object o,
Record record)
Add an object to the database
|
void |
rollback(java.sql.Connection c) |
java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
select(java.sql.Connection c,
java.lang.String sql)
Select a list of objects that match a given SQL statement
|
java.util.List<java.util.List<java.lang.String>> |
select(java.sql.Connection c,
java.lang.String sql,
boolean includeBlobs)
Select a list of objects that match a given SQL statement
|
java.util.List<java.util.List<java.lang.String>> |
select(java.sql.Connection c,
java.lang.String sql,
boolean includeBlobs,
java.lang.Object... params)
Select a list of objects that match a given SQL statement
|
java.util.List<java.lang.Object> |
select(java.sql.Connection c,
java.lang.String sql,
Record record,
boolean includeBlobs)
Select a list of objects that match a given SQL statement
|
java.util.List<java.lang.Object> |
select(java.sql.Connection c,
java.lang.String sql,
Record record,
boolean includeBlobs,
java.lang.Object... params)
Select a list of objects that match a given SQL statement
|
java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
select(java.lang.String sql)
Select a list of objects that match a given SQL statement
|
java.util.List<java.util.List<java.lang.String>> |
select(java.lang.String sql,
boolean includeBlobs)
Select a list of objects that match a given SQL statement
|
java.util.List<java.util.List<java.lang.String>> |
select(java.lang.String sql,
boolean includeBlobs,
java.lang.Object... params)
Select a list of objects that match a given SQL statement
|
java.util.List<java.lang.Object> |
select(java.lang.String sql,
Record record)
Select a list of objects through a given SQL statement
|
java.util.List<java.lang.Object> |
select(java.lang.String sql,
Record record,
boolean includeBlobs)
Select a list of objects through a given SQL statement
|
java.util.List<java.lang.Object> |
select(java.lang.String sql,
Record record,
boolean includeBlobs,
java.lang.Object... params)
Select a list of objects through a given SQL statement
|
void |
setDatabase(java.lang.String url)
Set the database
Use either this method together with method setDriverName or
method setDataSourceName to indicate the connection type
|
void |
setDatabase(java.lang.String driverName,
java.lang.String url)
Set the database
Use either this method or method setDataSourceName to indicate the connection type
|
void |
setDataSourceName(java.lang.String dataSourceName)
Set name of DataSource
Use either this method or method setDatabase to indicate the connection type
|
void |
setDriverName(java.lang.String driverName)
Set the JDBC driver class name
|
void |
startTransaction(java.sql.Connection c) |
java.util.List<java.util.List<java.lang.String>> |
toList(java.sql.ResultSet rs,
boolean includeBlobs)
Helper method that converts a ResultSet into a list of lists, one per row,
each row is a list of strings
|
java.lang.Object |
update(java.sql.Connection c,
java.lang.Object o)
update an object in the database
Use this method for single updates.
|
java.lang.Object |
update(java.sql.Connection c,
java.lang.Object o,
Record record)
update an object in the database
|
java.lang.Object |
update(java.lang.Object o)
update an object in the database
Use this method for single updates.
|
java.lang.Object |
update(java.lang.Object o,
Record record)
update an object in the database
|
public static final java.lang.String NULL_STR
protected javax.sql.DataSource ds
protected java.lang.String dburl
public PersistenceManager()
public void setDriverName(java.lang.String driverName) throws java.lang.ClassNotFoundException
driverName
- name of JDBC driver class namejava.lang.ClassNotFoundException
- when driver is not foundpublic void setDatabase(java.lang.String url)
Use either this method together with method setDriverName or method setDataSourceName to indicate the connection type
url
- JDBC urlpublic void setDatabase(java.lang.String driverName, java.lang.String url) throws java.lang.ClassNotFoundException
Use either this method or method setDataSourceName to indicate the connection type
driverName
- class name of JDBC driverurl
- JDBC urljava.lang.ClassNotFoundException
- when driver is not foundpublic void setDataSourceName(java.lang.String dataSourceName) throws javax.naming.NamingException
Use either this method or method setDatabase to indicate the connection type
dataSourceName
- name of DataSourcejavax.naming.NamingException
- when JNDI lookup failspublic java.sql.Connection getConnection()
public java.lang.Object delete(java.lang.Object o)
Use this method for single deletes. In cases where
several subsequent deletes for objects of the same class
are required the use of method delete(Object, Record)
is recommended instead to minimise instantiation
overhead.
o
- object to deletepublic java.lang.Object delete(java.sql.Connection c, java.lang.Object o)
Use this method for single deletes. In cases where
several subsequent deletes for objects of the same class
are required the use of method delete(Connection, Object, Record)
is recommended instead to minimise instantiation
overhead.
c
- the connection to use, expected to be open and establishedo
- object to deletepublic java.lang.Object delete(java.lang.Object o, Record record)
o
- object to deleterecord
- reference to object to use to map between object and databasepublic java.lang.Object delete(java.sql.Connection c, java.lang.Object o, Record record)
c
- the connection to use, expected to be open and establishedo
- object to deleterecord
- reference to object to use to map between object and databasepublic java.lang.Object insert(java.lang.Object o)
Use this method for single inserts. In cases where
several subsequent inserts for objects of the same class
are required the use of method insert(Object, Record)
is recommended instead to minimise instantiation
overhead.
o
- the object to addpublic java.lang.Object insert(java.sql.Connection c, java.lang.Object o)
Use this method for single inserts. In cases where
several subsequent inserts for objects of the same class
are required the use of method insert(Connection, Object, Record)
is recommended instead to minimise instantiation
overhead.
c
- the connection to use, expected to be open and establishedo
- the object to addpublic java.lang.Object insert(java.lang.Object o, Record record)
o
- object to addrecord
- reference to object to use to map between object and databasepublic java.lang.Object insert(java.sql.Connection c, java.lang.Object o, Record record)
c
- the connection to use, expected to be open and establishedo
- object to addrecord
- reference to object to use to map between object and databasepublic java.lang.Object update(java.lang.Object o)
Use this method for single updates. In cases where
several subsequent updates for objects of the same class
are required the use of method update(Object, Record)
is recommended instead to minimise instantiation
overhead.
o
- object to updatepublic java.lang.Object update(java.sql.Connection c, java.lang.Object o)
Use this method for single updates. In cases where
several subsequent updates for objects of the same class
are required the use of method update(Connection, Object, Record)
is recommended instead to minimise instantiation
overhead.
c
- the connection to use, expected to be open and establishedo
- object to updatepublic java.lang.Object update(java.lang.Object o, Record record)
o
- object to updaterecord
- reference to object to use to map between object and databasepublic java.lang.Object update(java.sql.Connection c, java.lang.Object o, Record record)
c
- the connection to use, expected to be open and establishedo
- object to updaterecord
- reference to object to use to map between object and databasepublic java.util.List<java.lang.Object> select(java.lang.String sql, Record record)
sql
- sql query string that designates the requested objectsrecord
- object to use to map db records to objectspublic java.util.List<java.lang.Object> select(java.lang.String sql, Record record, boolean includeBlobs)
sql
- sql query string that designates the requested objectsrecord
- object to use to map db records to objectsincludeBlobs
- true when BLOB contents are to be retrieved, false if notpublic java.util.List<java.lang.Object> select(java.lang.String sql, Record record, boolean includeBlobs, java.lang.Object... params)
sql
- sql query string that designates the requested objectsrecord
- object to use to map db records to objectsincludeBlobs
- true when BLOB contents are to be retrieved, false if notparams
- list of parameters in the order they appear in the SQL stringpublic java.util.List<java.lang.Object> select(java.sql.Connection c, java.lang.String sql, Record record, boolean includeBlobs)
c
- the database connection to use for this query, expected to be established and open alreadysql
- sql query string that designates the requested objectsrecord
- object to use to map db records to objectsincludeBlobs
- true when BLOB contents are to be retrieved, false if notpublic java.util.List<java.lang.Object> select(java.sql.Connection c, java.lang.String sql, Record record, boolean includeBlobs, java.lang.Object... params)
c
- the database connection to use for this query, expected to be established and open alreadysql
- sql query string that designates the requested objectsrecord
- object to use to map db records to objectsincludeBlobs
- true when BLOB contents are to be retrieved, false if notparams
- list of parameters in the order they appear in the SQL stringpublic java.util.List<java.util.Map<java.lang.String,java.lang.Object>> select(java.lang.String sql)
sql
- sql query string that designates the requested objectspublic java.util.List<java.util.List<java.lang.String>> select(java.lang.String sql, boolean includeBlobs)
sql
- sql query string that designates the requested objectsincludeBlobs
- true when content of blob coloumns should be returned, false if notpublic java.util.List<java.util.Map<java.lang.String,java.lang.Object>> select(java.sql.Connection c, java.lang.String sql)
c
- the database connection to use for this query, expected to be established and open alreadysql
- sql query string that designates the requested objectspublic java.util.List<java.util.List<java.lang.String>> select(java.sql.Connection c, java.lang.String sql, boolean includeBlobs)
c
- the database connection to use for this query, expected to be established and open alreadysql
- sql query string that designates the requested objectsincludeBlobs
- true when content of blob coloumns should be returned, false if notpublic java.util.List<java.util.List<java.lang.String>> select(java.lang.String sql, boolean includeBlobs, java.lang.Object... params)
sql
- sql query string that designates the requested objects with ? at the position of paramsincludeBlobs
- true when content of blob coloumns should be returned, false if notparams
- list of parameters in the order they appear in the SQL stringpublic java.util.List<java.util.List<java.lang.String>> select(java.sql.Connection c, java.lang.String sql, boolean includeBlobs, java.lang.Object... params)
c
- the database connection to use for this query, expected to be established and open alreadysql
- sql query string that designates the requested objects with ? at the position of paramsincludeBlobs
- true when content of blob coloumns should be returned, false if notparams
- list of parameters in the order they appear in the SQL stringpublic java.util.List<java.util.Map<java.lang.String,java.lang.Object>> executeWithKeys(java.lang.String sql)
sql
- the statement to executepublic java.util.List<java.util.Map<java.lang.String,java.lang.Object>> executeWithKeys(java.sql.Connection c, java.lang.String sql)
c
- database connection to usesql
- the statement to executepublic int execute(java.lang.String sql)
sql
- the statement to executepublic int execute(java.sql.Connection c, java.lang.String sql)
c
- database connection to usesql
- the statement to executepublic int execute(java.lang.String sql, java.lang.Object... params)
sql
- the SQL string with ? at the position of paramsparams
- list of parameters in the order they appear in the SQL stringpublic int execute(java.sql.Connection c, java.lang.String sql, java.lang.Object... params)
c
- database connection to usesql
- the SQL string with ? at the position of paramsparams
- list of parameters in the order they appear in the SQL stringpublic int[] executeScript(java.lang.String sqlScript)
sqlScript
- the SQL script to executepublic int[] executeScript(java.sql.Connection c, java.lang.String sqlScript)
c
- the Connection object to usesqlScript
- the SQL script to executepublic void startTransaction(java.sql.Connection c)
public void commit(java.sql.Connection c)
public void rollback(java.sql.Connection c)
public void closeResultSetFinally(java.sql.ResultSet rs)
rs
- the result set to closepublic void closeStatementFinally(java.sql.Statement s)
s
- the statement to closepublic void closeConnectionFinally(java.sql.Connection c)
c
- the connection to closepublic java.sql.PreparedStatement buildQuery(java.sql.Connection c, java.lang.String sql, java.lang.Object[] params) throws java.lang.Exception
sql
- the SQL string with ? at the position of paramsparams
- list of parameters in the order they appear in the SQL stringjava.lang.Exception
public java.util.List<java.util.List<java.lang.String>> toList(java.sql.ResultSet rs, boolean includeBlobs) throws java.sql.SQLException
rs
- result set to convertincludeBlobs
- true when blob columns should be returned, false if notjava.sql.SQLException