Posts Tagged ‘Java SE’
14
Feb

When you need to retrieve the plain Connection from JPA, unfortunately, there is no JPA standard way (unless you get a configured datasource with JNDI). This is needed when you use libraries or legacy applications which use JDBC instead of JPA.

Hibernate 3.x and JPA 1.0

With Hibernate you can get the org.hibernate.session. In pre Hibernate 4.o there was the connection() method which now is removed.

EntityManager em = ...;
Session session = (Session) em.getDelegate();
Connection conn = session.connection();

Hibernate 3.x and JPA 2.0

EntityManager em = ...;
Connection conn = em.unwrap(Session.class).connection();

Hibernate 4.x and JPA 2.0

Session session = em.unwrap(Session.class);
SessionFactoryImplementor sfi = (SessionFactoryImplementor) session.getSessionFactory();
ConnectionProvider cp = sfi.getConnectionProvider();
Connection conn = cp.getConnection();

JNDI-Lookup

If you are running inside a container, you could also perform a JNDI lookup on the configured DataSource.

, , , ,

24
Jan

If you have to deploy to several environments, like DEV, TEST, PROD etc., and you don’t want to edit the config and property files (e.g. db url connect string, data source names, logging props, etc.) over and over again, then maybe it is time :

  1. for each environment have a separate version of the config / property files.
  2. Dependent on the environment, load the right version.

For 2. you need some kind of environment detection. There are a lot of ways of achieving this:

web.xml

If you have a WebApp then you could provide a context parameter:

<context-param>
  <description>Environment name for dependent configuration/description>
  <param-name>org.bigdev.env</param-name>
  <param-value>PROD</param-value>
</context-param>

with

ServletContext servletContext = ...;
String env = servletContext.getInitParameter("org.bigdev.env");
if ("DEV".equals(env)) {
    // DEV
} else if ("TEST".equals(env)) {
    // TEST
} else if ("PROD".equals(env)) {
    // PROD
}

There you trade editing one property, instead of many in different files.

Property File or just a empty Trigger File

if ((new File("WEB-INF/env/DEV.trigger")).exists()) {
    // DEV
} else if ((new File("WEB-INF/env/TEST.trigger")).exists()) {
    // TEST
} else if ((new File("WEB-INF/env/PROD.trigger")).exists()) {
    // PROD
}

Host Name

String hostName = InetAddress.getLocalHost().getHostName();
if ("localhost".equals(hostName)) {
    // DEV
} else if ("srv007".equals(hostName)) {
    // TEST
} else if ("srv008".equals(hostName)) {
    // PROD
}

IP address or range

String ip = InetAddress.getLocalHost().getHostAddress();
if ("1.2.3.4".equals(ip)) {
    // DEV
} else if (ip.startsWith("2.3.4.")) {
    // TEST
} else if (ip.startsWith("2.3.5.")) {
    // PROD
}

Operating System

String osName = System.getProperty("os.name");
if (osName.startsWith("Windows")) {
    // DEV
} else if (osName.startsWith("Linux")) {
    // TEST
} else if (osName.startsWith("AIX")) {
    // PROD
}

System Property

String env = System.getProperty("org.bigdev.env");
if ("DEV".equals(env)) {
    // DEV
} else if ("TEST".equals(env)) {
    // TEST
} else if ("PROD".equals(env)) {
    // PROD
}

Or any other system property that identifies your env.

There are many more use cases of the knowledge of the environment other than config/property files. You can en-/disable certain behavior/features of your application dependent on the current environment. E.g. in JPA overwrite the annotation object-relational mapping via orm.xml dependent on the env, see  here.

, ,

18
Jan

To install and run a HSQLDB in server mode proceed as follows:

  • Download the release from http://hsqldb.org.
  • Unzip it, e.g. C:\hsqldb-2.2.5\.
  • The HSQLDB-jar is then found at C:\hsqldb-2.2.5\hsqldb\lib\hsqldb.jar.
  • Create a batch file for starting the HSQLDB in server mode, e.g. C:\hsqldb-2.2.5\hsqldb\bin\hsqldbServer_start.bat with content:

@java -classpath ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:../data/MH --dbname.0 MH_DB

  • This creates and starts a db with name MH_DB with db-files in the directory C:\hsqldb-2.2.5\hsqldb\data. You can start several databases at once, replace the batch file content with:

@java -classpath ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:../data/MH1 --dbname.0 MH_DB1 --database.1 file:../data/MH2 --dbname.1 MH_DB2

  • You then can reach the HSQLDB via JDBC / JPA

driver: 'org.hsqldb.jdbcDriver'
url: 'jdbc:hsqldb:hsql://localhost/MH_DB'
user: 'SA'
pwd: ''

  • When using JPA with Hibernate then you can specify a dialect in persistence.xml (for other JPA providers, check the documentation):

<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />

, , , , , ,

11
Jan

Searching for artifacts to add as dependencies to your maven projects sometimes requires one to spend too much time with Google ;-)
Especially when you need a specific jar, but don’t know its groupId, artifactId, or the latest version that is available.
That’s why I list those links:

Search Engines
http://search.maven.org – official central Maven repository search
http://www.jarvana.com – recommended! also a JavaDoc Search…
http://mvnrepository.com
http://www.artifact-repository.org
http://www.mvnbrowser.com

Repositories
http://repo1.maven.org – central Maven repository (no direct browsing, use a search engine above for browsing instead)
https://repository.apache.org – Apache Nexus repository
https://repository.jboss.org/nexus – JBoss Nexus repository
https://repository.sonatype.org – Sonatype Nexus repository

So go find your jars…

, ,