public class SEHRDataSource
extends java.lang.Object
To access a SEHR database there are three basic, but different ways
1) using the ORM layer Hibernate (native w/o JPA)
2) accessing the
database by using driver connections and 'java.sql' methods
3) using
JNDI and/or pooled connections
In any case the database must be assigned. This object can be used either in
SEHR server environments where the connection parameters are available by the
SEHRConfiguration
configuration object or in a client environment by
setting the parameters during object initialization or by using the
parameterless constructor with some setter methods.
To use
JNDI/pooled connections do
initFBDS()
connect()
to establish a
connectionTo initialize ORM do
#SEHRDataSource(org.ifeth.sehr.core.lib.SEHRConfiguration)
orSEHRDataSource()
and then
init(org.ifeth.sehr.core.lib.SEHRConfiguration)
To use JPA with or w/o Hibernate you probably do not need this class. The JPA does not need a 'hibernate.xml.conf' and all classes are defined by the 'persistence.xml' as well as the connection. The entities of SEHR are based on EJB 3.0 and JPA annotations so that these are usable also in JPA.
Modifier and Type | Field and Description |
---|---|
static int |
DBPORT |
static java.lang.String |
LC_CTYPE |
static java.lang.String |
XMLCONF |
Modifier | Constructor and Description |
---|---|
protected |
SEHRDataSource()
Initiates a parameterless object instance for database connection.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the current firebird connection handle...
|
void |
close(int idx)
Close a given connection handle.
|
static void |
close(SEHRDataSource fbds)
Close connection of the given data source handle.
|
void |
closeAll()
Deprecated.
use
destroy() |
static void |
closeConnection(java.sql.Connection connection)
Helper method to close a given SQL connection in other modules, nothing
else.
|
void |
closeFBDataSource()
Deprecated.
use
close() |
static void |
closeFBDataSource(SEHRDataSource fbds)
Deprecated.
use
close() |
void |
closeJPAEntityManager()
Close the JPA (RESOURCE_LOCAL) instance for managing the entities.
|
static void |
closeResultSet(java.sql.ResultSet rs) |
static void |
closeStatement(java.sql.Statement st) |
void |
configure(java.util.Properties p)
Configure a native (FBDS) access to a firebird database.
|
java.sql.Connection |
connect()
Make a (re-)connection using the current object assuming all required
parameters are already invoked.
|
java.sql.Connection |
connectFBDataSource(SEHRDataSource fbds,
java.lang.String host,
java.lang.String port,
java.lang.String database,
java.lang.String dbuser,
java.lang.String dbpw)
Deprecated.
Use
getConnection() |
int |
countPooledConnections()
Get the number of currently pooled and used connections by pooled
datasource.
|
void |
destroy()
Close all registered pooled connections, close the current connection and
close ORM if any.
|
java.sql.Connection |
getConnection()
Return a connection handle.
|
java.sql.Connection |
getConnection(int idx)
Get an already established connection from map by index.
|
int |
getConnectionIndex()
Retrieve current used or last registered connection id (its hashcode).
|
java.lang.String |
getDBFile() |
java.lang.String |
getDBHost()
For general usage: get host of db access
|
java.lang.String |
getDBPass() |
int |
getDBPort() |
java.lang.String |
getDBUser() |
org.firebirdsql.ds.FBConnectionPoolDataSource |
getFBDS() |
protected java.sql.DatabaseMetaData |
getFBMetaData()
Get metadata about the current database source.
|
protected java.sql.DatabaseMetaData |
getFBMetaData(SEHRDataSource fbds)
Get metadata object about the given database source.
|
java.util.List<java.lang.Object> |
getGenerator()
Show the generator id's of the initialized native DB access.
|
java.util.List<java.lang.Object> |
getGenerator(java.lang.String genid)
Show the value of the given generator of the initialized native DB access.
|
static SEHRDataSource |
getInstance() |
javax.persistence.EntityManager |
getJPAEntityManager()
Get (or create) instance for managing the entities.
|
javax.persistence.EntityManagerFactory |
getJPAHibernateFactory() |
java.lang.String |
getRandomID()
Get a random digest ID.
|
SEHRConfiguration |
getSEHRCfg() |
int |
initConnection()
Init a new connection from pool and store it locally in a map.
|
org.firebirdsql.ds.FBConnectionPoolDataSource |
initFBDS()
Init a pooled data source to be used also by ORM/Hibernate/JPA.
|
void |
initialize(java.util.Properties p)
Initiates an object instance on a SEHR host using given properties.
|
void |
initialize(SEHRConfiguration cfg)
Initiates an object instance on a SEHR host using the SEHR configuration
object.
|
void |
initialize(java.lang.String host,
int port,
java.lang.String database,
java.lang.String dblogin,
java.lang.String dbpw,
java.lang.String ORMFile)
Deprecated.
Use either
#SEHRDataSource(SEHRConfiguration) or
#SEHRDataSource(Properties) |
void |
initialize(java.lang.String host,
java.lang.String port,
java.lang.String database,
java.lang.String dblogin,
java.lang.String dbpw)
Deprecated.
Use either
#SEHRDataSource(SEHRConfiguration) or
#SEHRDataSource(Properties) |
void |
initialize(java.lang.String host,
java.lang.String sport,
java.lang.String database,
java.lang.String dblogin,
java.lang.String dbpw,
java.lang.String ORMFile)
Deprecated.
Use either
#SEHRDataSource(SEHRConfiguration) or
#SEHRDataSource(Properties) |
javax.persistence.EntityManagerFactory |
initJPAResourceLocal() |
boolean |
isConnection()
Check status of current connection.
|
boolean |
isFBPooledDS()
Get status if a pooled datasource is present.
|
boolean |
isJPAResourceLocal(javax.persistence.EntityManager em) |
static org.jdom.Document |
loadXMLFile(java.io.File file,
DeEncrypter denc)
Read a XML file object and prepare a document object (DOM)
|
static org.jdom.Document |
loadXMLFile(java.lang.String file)
Read a XML file and prepare a document object (DOM)
|
javax.sql.PooledConnection |
pooledConnection()
Create a pooled connection for current firebird database source object.
|
static java.lang.Object |
readBLOBField(SEHRConfiguration sehrcfg,
java.lang.String table,
java.lang.String field,
java.lang.String pkfield,
int recid)
Deprecated.
We trust only our datasource instance ...
|
static java.lang.Object |
readBLOBField(SEHRDataSource ds,
java.lang.String table,
java.lang.String field,
java.lang.String pkfield,
int recid)
Retrieve a BLOB field...
|
static byte[] |
readBytesFromFile(java.io.File file)
Method to get contents of a file as bytes.
|
static java.lang.Object |
readDataObjectFile(java.io.File file,
DeEncrypter cipher)
Reads/Loads an object from file.
|
void |
setCfg(SEHRConfiguration cfg)
Set cfg object to use for datasource initialization.
|
void |
setDataBase(java.lang.String dbHost,
int dbPort,
java.lang.String dbFile,
java.lang.String dbUser,
java.lang.String dbUserPass)
For client usage: set database including host, port, path_to_db_file and
login.
|
void |
setDBFile(java.lang.String sFBDataBase) |
void |
setDBHost(java.lang.String host)
For client usage: set port of firebird engine
|
void |
setDBLogin(java.lang.String sFBLogin)
Deprecated.
use setDBUser(user)
|
void |
setDBPass(java.lang.String pw) |
void |
setDBPort(int port)
For client usage: set port of firebird engine
|
void |
setDBPw(java.lang.String pw)
For client usage: set database login password
|
void |
setDBUser(java.lang.String user)
For client usage: set database login name
|
void |
setFBDS(org.firebirdsql.ds.FBConnectionPoolDataSource fbpds) |
void |
setJPAHibernateFactory(javax.persistence.EntityManagerFactory emf) |
java.lang.String |
showProcedures() |
static java.lang.String |
showProcedures(SEHRDataSource fbds)
Show the procedures of the initialized native DB access.
|
java.lang.String |
showTables()
Show the tables of the assigned DB.
|
java.lang.String |
showTables(boolean showfields)
Show the tables of the initialized native DB access.
|
static java.lang.String |
showTables(SEHRDataSource fbds)
Show the tables of the initialized native DB access.
|
static java.lang.String |
showTables(SEHRDataSource fbds,
boolean showfields)
Show the tables of the initialized native DB access.
|
java.lang.String |
toString() |
static boolean |
writeBLOBRecord(SEHRConfiguration sehrcfg,
java.lang.String table,
java.lang.String field,
java.lang.String pkfield,
long recid,
java.lang.Object o)
Deprecated.
Code cleaning... use datasource directly...
|
static boolean |
writeBLOBRecord(SEHRDataSource ds,
java.lang.String table,
java.lang.String field,
java.lang.String pkfield,
long recid,
java.lang.Object o)
Writes a BLOB field to the database table.
|
static boolean |
writeDataObjectFile(java.io.File file,
java.lang.Object object,
DeEncrypter cipher)
Writes an object to file
|
static boolean |
writeStreamToFile(java.io.InputStream is,
java.lang.String filelocation) |
static boolean |
writeXMLFile(java.io.OutputStream out,
org.jdom.Document doc)
Writes a DOM (XML parsed) document to given output stream (System.out, File
or URL)
|
public static int DBPORT
public static java.lang.String XMLCONF
public static java.lang.String LC_CTYPE
protected SEHRDataSource()
To access the IntraSEC db use either
or initialize a connection
step by step (e.g. on clients) by using the following setter methods
followed by #SEHRDataSource(Properties)
initFBDS()
:
SEHRDataSource
or
,
setDBFile(String)
,
setDBHost(String)
,
setDBPort(int)
,
setDBLogin(String)
.
setDBPw(String)
For Hibernate with EJB3 use the 'JTA' configuration of Glassfish.
public static SEHRDataSource getInstance()
public void initialize(java.util.Properties p)
Notice: If 'jndi.*' keys are defined the firebird datasource will be stored as a pooled connection as well as Hibernate will use this datasource for the session (Hibernate will be initialized if any 'ORM.*' key is present).
p
- This Properties
object holds all required data to
connectpublic void initialize(SEHRConfiguration cfg)
Important security note:
Due to security reasons the username
and password to access any SEHR database (IntraSEC and IntraDOC etc.) are
not stored by the configuration object. So you must know them and set the
username and password using the corresponding setters. To use this
component on the server site use JNDI and retrieve the object
FBConnectionPoolDataSource to get a ready for use access.
cfg
- The SEHRConfiguration object tracks all required data to connectpublic void initialize(java.lang.String host, int port, java.lang.String database, java.lang.String dblogin, java.lang.String dbpw, java.lang.String ORMFile)
#SEHRDataSource(SEHRConfiguration)
or
#SEHRDataSource(Properties)
This constructor is defined for clients, where a local file access to a
SEHR configuration SEHRConfiguration
or ORM configuration is not
available. There are two ways for the client to handle the parameters for a
connection:
setFBDS(org.firebirdsql.ds.FBConnectionPoolDataSource)
For a db connection use
.
connect()
host
- port
- database
- dblogin
- dbpw
- ORMFile
- Configuration file for Hibernate (path + filename)public void initialize(java.lang.String host, java.lang.String sport, java.lang.String database, java.lang.String dblogin, java.lang.String dbpw, java.lang.String ORMFile)
#SEHRDataSource(SEHRConfiguration)
or
#SEHRDataSource(Properties)
host
- sport
- database
- dblogin
- dbpw
- ORMFile
- Configuration file for Hibernate (path + filename)SEHRDataSource#SEHRDataSource(Properties)
public void initialize(java.lang.String host, java.lang.String port, java.lang.String database, java.lang.String dblogin, java.lang.String dbpw)
#SEHRDataSource(SEHRConfiguration)
or
#SEHRDataSource(Properties)
To connect/open use connect()
host
- port
- database
- dblogin
- dbpw
- public void configure(java.util.Properties p)
p
- public org.firebirdsql.ds.FBConnectionPoolDataSource initFBDS()
By JDBC convention apps should use this kind of data source only. The ds object canbe stored by JNDI for a common reusage through the app.
public javax.persistence.EntityManagerFactory initJPAResourceLocal()
public java.sql.Connection connectFBDataSource(SEHRDataSource fbds, java.lang.String host, java.lang.String port, java.lang.String database, java.lang.String dbuser, java.lang.String dbpw) throws java.sql.SQLException
getConnection()
fbds
- host
- port
- database
- dbuser
- dbpw
- java.sql.SQLException
public java.sql.Connection connect()
public void closeFBDataSource() throws java.sql.SQLException
close()
java.sql.SQLException
public static void closeFBDataSource(SEHRDataSource fbds) throws java.sql.SQLException
close()
fbds
- java.sql.SQLException
public void close()
public void close(int idx) throws java.sql.SQLException
idx
- - the hash code of a connectionjava.sql.SQLException
public static void close(SEHRDataSource fbds) throws java.sql.SQLException
fbds
- FBDataSource object @deprecated use close()
java.sql.SQLException
public static void closeResultSet(java.sql.ResultSet rs)
public static void closeStatement(java.sql.Statement st)
public static void closeConnection(java.sql.Connection connection)
connection
- public javax.sql.PooledConnection pooledConnection() throws java.sql.SQLException
Note: Use closeConnection(int conidx)! Pooled connections do have
large benefits in c/s connections but there are also some traps. One of
these traps is the number and the maximum of pooled concurrent connections.
To get an overview of the number of used connections use
countPooledConnections()
.
To close all
connections at once use closeAll()
To get a
specific and already established connection use
getConnection(int idx)
java.sql.SQLException
public int initConnection()
public java.sql.Connection getConnection(int idx)
To keep the integrity of prepared statements the pool is indexed. So you may init a connection, assign statements to it and use exactly the indexed connection to get your statementents back.
idx
- public int getConnectionIndex()
getConnection(int)
with the hashcode as parameter. Another use case is to close a DB
connection using close(int)
to return the handle back into the
pool.public void closeAll()
destroy()
public void destroy()
public int countPooledConnections()
Note: Only available on pooled connections ...
public boolean isFBPooledDS()
public boolean isConnection()
public java.sql.Connection getConnection()
If there is no connection this method tries to establish a native JDBC connection by using the settings.
public java.lang.String getRandomID()
public void setCfg(SEHRConfiguration cfg)
cfg
- public void setDBPort(int port)
port
- The port of the database (if <1024 port it is set to 3050)public java.lang.String getDBHost()
public void setDBHost(java.lang.String host)
host
- Use IP or DNS to connectpublic void setDataBase(java.lang.String dbHost, int dbPort, java.lang.String dbFile, java.lang.String dbUser, java.lang.String dbUserPass)
dbHost
- dbPort
- dbFile
- dbUser
- dbUserPass
- public void setDBUser(java.lang.String user)
public void setDBPw(java.lang.String pw)
public void setFBDS(org.firebirdsql.ds.FBConnectionPoolDataSource fbpds)
public org.firebirdsql.ds.FBConnectionPoolDataSource getFBDS()
public int getDBPort()
public java.lang.String getDBFile()
public void setDBFile(java.lang.String sFBDataBase)
sFBDataBase
- the sFBDataBase to setpublic java.lang.String getDBUser()
public void setDBLogin(java.lang.String sFBLogin)
sFBLogin
- the sFBLogin to setpublic java.lang.String getDBPass()
public void setDBPass(java.lang.String pw)
pw
- the password to the DB to setpublic void setJPAHibernateFactory(javax.persistence.EntityManagerFactory emf)
public javax.persistence.EntityManagerFactory getJPAHibernateFactory()
public javax.persistence.EntityManager getJPAEntityManager()
public void closeJPAEntityManager()
Note:Do not close JTA container managed instances!
public boolean isJPAResourceLocal(javax.persistence.EntityManager em)
public static byte[] readBytesFromFile(java.io.File file) throws java.io.IOException
This method is used to prepare an object to store as a BLOB
file
- java.io.IOException
public static java.lang.Object readDataObjectFile(java.io.File file, DeEncrypter cipher)
file
- cipher
- public static boolean writeStreamToFile(java.io.InputStream is, java.lang.String filelocation)
public static boolean writeDataObjectFile(java.io.File file, java.lang.Object object, DeEncrypter cipher)
file
- object
- cipher
- public static org.jdom.Document loadXMLFile(java.lang.String file)
file
- public static org.jdom.Document loadXMLFile(java.io.File file, DeEncrypter denc)
file
- denc
- 'null' if the file is not cryptedpublic static boolean writeXMLFile(java.io.OutputStream out, org.jdom.Document doc)
out
- doc
- public static java.lang.Object readBLOBField(SEHRConfiguration sehrcfg, java.lang.String table, java.lang.String field, java.lang.String pkfield, int recid)
public static java.lang.Object readBLOBField(SEHRDataSource ds, java.lang.String table, java.lang.String field, java.lang.String pkfield, int recid)
ds
- table
- field
- pkfield
- recid
- public SEHRConfiguration getSEHRCfg()
public static boolean writeBLOBRecord(SEHRConfiguration sehrcfg, java.lang.String table, java.lang.String field, java.lang.String pkfield, long recid, java.lang.Object o)
public static boolean writeBLOBRecord(SEHRDataSource ds, java.lang.String table, java.lang.String field, java.lang.String pkfield, long recid, java.lang.Object o)
public java.lang.String toString()
toString
in class java.lang.Object
protected java.sql.DatabaseMetaData getFBMetaData()
protected java.sql.DatabaseMetaData getFBMetaData(SEHRDataSource fbds)
fbds
- Firebird datasource object with a native access configured!public java.lang.String showTables()
public java.lang.String showTables(boolean showfields)
public static java.lang.String showTables(SEHRDataSource fbds)
public static java.lang.String showTables(SEHRDataSource fbds, boolean showfields)
fbds
- showfields
- public java.lang.String showProcedures()
public static java.lang.String showProcedures(SEHRDataSource fbds)
fbds
- public java.util.List<java.lang.Object> getGenerator()
public java.util.List<java.lang.Object> getGenerator(java.lang.String genid)
genid
-