Showing posts with label JPA. Show all posts
Showing posts with label JPA. Show all posts

Wednesday, October 28, 2009

Use c3p0 connection pooling with JPA

Add the following to your POM:


<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.3.2.GA</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>3.2.3.GA</version>
</dependency>

<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>

Add the following to your persistence.xml:


<property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="300"/>

You can verify c3p0 is working by using jconsole (in your java/bin) and connecting to your running Tomcat instance. If jmx isn't enabled, you can enable it by providing the following JAVA_OPTS in catalina.sh:

-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=3000
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

Wednesday, September 16, 2009

Using Oracle Sequences with JPA

Oracle doesn't allow auto increment like MySQL. Auto increment, on big applications can cause all sorts of problems. Most JPA examples are targeted for MySQL.

First, I use Number(19) for id columns.

Second, the generator name is not important so long as it is unique and generator in GeneratedValue annotation matches name in SequenceGenerator annotation.

Sequence Name in SequenceGenerator is the name of your Oracle sequence.

Example entity code:

import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.SequenceGenerator;
...
@Id
@Basic(optional = false)
@Column(name = "MY_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "MYNOTIFSEQ")
@SequenceGenerator(name="MYNOTIFSEQ", sequenceName = "MY_SEQ")
private Long userNotificationId;
...


Here's a sample sequence:
------------------------------------
--
-- Sequence MY_SEQ
--
------------------------------------
CREATE SEQUENCE MY_SEQ
INCREMENT BY 1
START WITH 1
MINVALUE 1
MAXVALUE 999999999999999999
NOORDER
CACHE 20
CYCLE
/

Thursday, July 30, 2009

Using Named Queries

Named queries make it easy to reuse SQL queries in JPA Entity Beans & Controllers and they are also more readable than inline SQL. I'm not sure, but my guess is they probably run faster too.

There are two steps.

1. Add Named Queries as a comma delimited list before your Entity bean's class definition, like so (Remember that the queries are in QL, not SQL. It's SQL like syntax, but you're manipulating the Java objects, not the underlying SQL, so name your "tables" and "columns" appropriately):
@Entity
@Table(name = "MY_TABLE", catalog = "", schema = "")
@NamedQueries({
@NamedQuery(name = "SchedJob.findAll", query = "SELECT s FROM SchedJob s"),
...
)})
public class MyObject implements Serializable {
...


2. Use the named query in your controller by calling createNamedQuery on your entity manager.
    public List findByText(String text) {
EntityManager em = getEntityManager();
try {
Query q = em.createNamedQuery("MyObject.findByText");
q.setParameter("text", text);
return q.getResultList();
} finally {
em.close();
}
}

You can also return a single result and some other things (Google for the Javadocs).

A more compact format is:
Teachers teacher = (Teachers) 
em.createNamedQuery("Teachers.findByGName").setParameter("gName",
gNameString).getSingleResult();

or
List teachers = (Teachers) 
em.createNamedQuery("Teachers.findByGName").setParameter("gName",
gNameString).getResultList();

Labels

Blog Archive

Contributors