Showing posts with label Pending. Show all posts
Showing posts with label Pending. Show all posts

Wednesday, January 6, 2010

When out of memory

Enable the JVM's ability to dump the heap upon error and then analyze it with IBM HeapAnalyzer or Eclipse MAT.  To do so, add +-XX:+HeapDumpOnOutOfMemoryError to the JVM parameter line (or use jmap to force a heap dump if you know when it's close to dying).

+

You can also enable verbose GC logging to get a better sense of what the heap is doing:

-verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -Xloggc:<gc-log-location.log>

Friday, May 15, 2009

How to filter collection in JPA/JPQL?

I have two entities:

@Entity
public class Customer  implements java.io.Serializable {
...
    @OneToMany(fetch=FetchType.EAGER, mappedBy="customer")
    private Set<CustomerOrder> customerOrders;
...


@Entity
public class CustomerOrder  implements java.io.Serializable {
....       

    private double cost;

    @ManyToOne
    @JoinColumn(name="CUST_ID")
    public Customer customer;
...


Now in my JPQL, I want to return those customers with their CustomerOrder.cost>1000. For example, there are three customers A, B and C. A has two orders with cost=1000 and 2000 respectively. B has three orders with cost=2000,3000 and 500 respectively. C has one order with cost=500. Now i want to get the three customers: A returns the orders with cost=2000 only; B returns the orders with 2000 and 3000; C returns an empty orders collection.



But the following will always return the full collection:



select c from Customer c, in(c.customerOrders) o where o.cost>1000


How can I do that in JPQL or in Hibernate in particular?

Monday, May 4, 2009

What is Terracotta

Terracotta is Network-Attached Memory

Terracotta is open source infrastructure software that makes it inexpensive and easy to scale a Java application to as many computers as needed, without the usual custom application code and databases used to share data in a cluster.

Terracotta manages mission critical data using Network-Attached Memory (NAM) technology. NAM enables Terracotta to cluster Java Virtual Machines (JVMs) directly underneath applications, and is a proven runtime approach to providing Java applications both high availability and scalability.

http://www.terracotta.org/web/display/orgsite/Home

Wednesday, February 25, 2009

Diagnosing Memory Leaks Using Heap Dumps, jmap, and jhat

JConsole is a great tool for detecting memory leaks. Once you have detected a memory leak, however, you need to look further to identify what classes and objects are being leaked, and jConsole is of limited use here. Profiling tools can be a good help here, but tend to be complicated to set up and use. We look at a few profiling tools in this chapter. If you need a quick diagnosis, one alternative is to use some of the tools bundled with the JDK 6 such as jmap and jhat. In this section, we will go through how to use these tools to hunt down memory leaks by analyzing the JVM heap with these tools.

If you just want to take a quick glance at the heap of a running application, you can use the jmap command-line tool to find the PID of the Java application you are interested in (see Section 18.2), and run the jmap command with the -histo option, as follows:

$ jmap -histo 1060
num #instances #bytes class name
--------------------------------------
1: 97929 4769472 [Ljava.lang.Object;
2: 40390 4685944 <constMethodKlass>
3: 116050 4642000 com.equinox.jpt.modelplanes.core.domain.ModelPlane
4: 40390 3232240 <methodKlass>
5: 164315 2629040 java.lang.Long
6: 4862 2597352 [B
7: 44929 2516024 org.hibernate.engine.EntityEntry
8: 53272 2369464 <symbolKlass>
9: 4217 2182784 [I
10: 89833 2155992 java.util.HashMap$Entry
...


This will list the number of instances and total size occupied by objects of each class, sorted by size. This can sometimes give you some good leads. For example, if you see any of your own classes in the top 10, it's probably a bad sign and should be investigated further.

Using jmap alone can be a good first approach, but it has its limits. A more sophisticated approach is to use jhat. The jhat command-line tool, new to Java 6, is a powerful way of investigating the JVM heap. The first thing you need is a heap dump to analyze. One way is to use the jmap command-line tool, which can obtain the heap dump of a running Java application. Find the PID of the Java application you are interested in (see Section 18.2), and run the jmap command with the -dump option, as follows:



$ jmap -dump:file=dump.bin 1060
Dumping heap to /home/john/dump.bin ...
Heap dump file created

This will generate a binary dump of the JVM heap in a file called dump.bin.

Another option if you suspect memory leaks on a production is to start your application with the -XX:+HeapDumpOnOutOfMemoryError command-line option. This won't prevent any memory leaks, but it will cause the VM to generate a heap dump, enabling you to analyze the heap afterward, using jhat or some other tool.



Now we need to be able to inspect the contents of the heap dump. This is where jhat comes into action. It analyzes a binary heap dump, and starts up a web server on a local port where you can interactively explore and query the Heap Dump. You run it as follows (the -J-Xmx384m allows a maximum heap space of 384 MB; this option is not mandatory, but jhat is fairly demanding in resources, so you should give it a fair bit of memory to work with):



$ jhat -J-Xmx384m dump.bin
Reading from dump.bin...
Dump file created Tue Dec 26 13:20:27 NZDT 2006
Snapshot read, resolving...
Resolving 949898 objects...
Chasing references, expect 189 dots.............................................
................................................................................
................................................................
Eliminating duplicate references................................................
................................................................................
.............................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.



You can now connect to this site with your favorite browser. The jhat web site won't win any prizes for its elegant design or usability, but it can provide some useful information. The first place to look is the heap histogram, which provides a list of the objects in the Heap (see Figure 18-9). This is similar to the jmap histogram, but with the additional possibility to sort by size, class, or object count, and to display the class details. In Figure 18-9, it is fairly easy to see that there may be an issue with the ModelPlane. (This is of course a simple example where a memory leak was inserted on purpose; memory leaks involving Strings or other commonly used classes are usually much harder to isolate.)





Figure 18-9. The jhat class histogram



image



If you click on one of the classes in this page, you will go to the class details page, which displays general information about the class (superclasses, subclasses, members, and so forth), as well as a list of classes that refer to this class. This page also lets you build root reference chains, which are arguably the most useful feature of jhat. You will find these under "Reference Chains from Rootset," in a section inconspicuously entitled "Other Queries" at the bottom of the screen. A reference chain lists the references to this object, going back up to the root thread (see Figure 18-10). The heap histogram can indicate which objects have been leaked. Using these chains, you can get a fairly good idea of where these leaked objects are being held.





Figure 18-10. A root reference query



image

Monday, February 9, 2009

OpenJPA Caching

The JPA specification defines two levels of caching: one that is scoped to the Entity Manager and another that is scoped to the Persistent Unit. The Entity Manager level cache is associated with a current transaction unless you are using the Extended Context—in which you can scope the cache to the life cycle of a Stateful Session Bean. We discussed this life cycle earlier in the "Programming Model" section. The JPA Entity Manager has a flush method you can invoke to clear the cache and push changes to the database.

The Entity Manager cache is there is for keeping data around during a multi-request flow, often called a "conversation;" however, the persistence unit cache is meant to really be a performance booster for read-only or read-mostly data. OpenJPA provides a data-level cache at the persistence unit level. OpenJPA provides a Single JVM cache provider. This may be ideal for read-only data that is initialized during the startup of an application.

For Example:
<property name="openjpa.DataCache" value="true(CacheSize=5000, SoftReferenceSize=0)"/>
<property name="openjpa.QueryCache" value="CacheSize=1000, SoftReferenceSize=100"/>
<property name="openjpa.RemoteCommitProvider" value="sjvm"/>


"svjm" here means "single jvm". Is there any other option here? How to use caching in distributed environment?

Friday, December 19, 2008

Mercurial - distributed version control

Pull changes from another machine
hg pull http://ip:port


After pull, everything is in local repository. To copy it to your workspace,
hg update
It will copy files from repository to local workspace.

To check the branches
hg branches


Before commit, you'd better check what is changed
hg status -mard


To commit
hg commit -m "message..." fileName

If filename is not provided, it will check in all changes.

To delete a file in repository
hg remove -f filename
hg commit -m "message" filename



Hmmm...... Mercurial does not support checking in empty directory...

Saturday, September 20, 2008

Difference between static inner class and non-static inner class

Inner class

Could non-anonymous inner class or anonymous inner class be defined in
  • another class
  • interface
  • a method


=====================================================================

Here is one example from my project. We have an abstract class AbstractCatalogViewNeedChangeSetAction. It has a couple of sub classes, CreateCategoryAction, DeleteCategoryAction ... We want all the sub classes be registered to an publisher. The code to register to an publish is put in the abstract class using an inner class.
public abstract class AbstractCatalogViewNeedChangeSetAction {
public AbstractCatalogViewNeedChangeSetAction() {
super();
addObjectRegistryListener();
//each sub class construct should call super() so that it will be registered to the publisher.
}

private void addObjectRegistryListener() {
//ObjectRegistryListener is the interface of the listener.
ObjectRegistryListener objectRegistryListener = new ObjectRegistryListener() {
public void objectAdded(final String key, final Object object) {
setEnabled(canBeEnabled());
}

public void objectRemoved(final String key, final Object object) {
setEnabled(canBeEnabled());
}

public void objectUpdated(final String key, final Object oldValue, final Object newValue) {
setEnabled(canBeEnabled());
}
};

//ObjectRegistry is the publisher
ObjectRegistry.getInstance().addObjectListener(objectRegistryListener);
}

//this is the method should be implemented by different sub class.
protected abstract boolean canBeEnabled();

Friday, August 8, 2008

SOA Security

http://it.toolbox.com/blogs/the-soa-blog/soa-security-architecture-11431

Thursday, August 7, 2008

Unit testing, regression testing and integration testing

Regression Testing

Any time you modify an implementation within a program, you should also do regression testing. You can do so by rerunning existing tests against the modified code to determine whether the changes break anything that worked prior to the change and by writing new tests where necessary.

Monday, June 16, 2008

JSF How to deal with "session" or "cookie"

Let's say after user login, we want to keep the status that the user is already login. The status could be saved in cookie or session. I need some sample code to touch the base of it.