Postgres writing / reading data from file

Output result of a select query to a file using \o 
1) \o /tmp/mydata.txt
2) select * from some_table;


You will notice that result in the /tmp/mydata.txt file

Read content of a file for updating or inserting
1) Set the content of a file to the variable
\set content = `cat /tmp/read_content.text`
2) refer this variable in query
update my_table set data = :'content' where id = 411;

Hibernate : ManyToOne getId eagerly and avoid saving ManyToOne object


Today I found good trick to achieve this.

i.e. directly use ManyToOne to getId
and to get the full object lazily loaded
and avoid saving the same object


It is from blog post
http://stackoverflow.com/questions/1900788/hibernate-manytoone-save-org-hibernate-transientobjectexception


ManyToOne(fetch=FetchType.EAGER)
@**JoinColumn**(name = "DEPARTMENT_ID", referencedColumnName = "DEPARTMENT_ID", **insertable=false, updatable=false**)
private Department department;

@Column(name = "department_id")
private Long departmentId;

Make sure to annotate all fields with @Column annotation with the name, otherwise hibernate will throw exception

Dumping a java object's properties


From this stackoverflow question and answer,

I got the answer which is very useful to dump properties of a java object.

Mostly useful for debugging.

org.apache.commons.lang3.builder.ReflectionToStringBuilder.toString(object);

Running Google Chrome as root


Open Terminal and run below commands 
1)xhost +
2) sudo -i -u google-chrome

When done

3) xhost - 

Taking thread/heap dump of running server/process


2. After extracting the jdk , run the following command to take the dump 
/bin/jmap -dump:format=b,file=./heapdump PID

3. To take only thread dump
./bin/jstack -l PID 
./bin/jstack -l PID > /tmp/thread_dump.txt


Following linux commands can be used for digging 

awk '/State: B/ { print }' < /tmp/thread_dump.txt  | sort | uniq -c
awk '/State: WAITING/ { getline; print }' < /tmp/thread_dump.txt | sort | uniq -c

Ways to debug apache tomcat


1) Start tomcat with this command 
~/apache-tomcat-7.0.47/bin/catalina.sh jpda start -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

OR 

2) export CATALINA_OPTS='-Djava.awt.headless=true -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000'


And attache IDE to 8000 remote process.