Friday, February 13, 2009

Determining which Process is Using a Port

This post is more of a reminder to myself than anything. To determine which process is using a port with fuser in Linux run:
fuser -n tcp port
Alternatively, you can use netstat:
netstat -nlp | grep port

Wednesday, February 11, 2009

JPA: Attempted to deploy PersistenceUnit while being in the wrong state

For a while I would see the following exception at random times in logs while using JPA in Spring.:

org.springframework.transaction.CannotCreateTransactionException:
Could not open JPA EntityManager for transaction;
nested exception is javax.persistence.PersistenceException:
Exception [TOPLINK-28013] (Oracle TopLink Essentials - 2.1 (Build 60 (11/17/2008))):
oracle.toplink.essentials.exceptions.EntityManagerSetupException
Exception Description: Attempted to deploy PersistenceUnit [harborPU]
while being in the wrong state [Undeployed].
Close all factories for this PersistenceUnit.
Turns out the
PersistenceUnit would be left in the wrong state if the EntityManagerFactory in the JpaTemplate has not been closed. I assumed Spring would take care of this for me, but the behaviour wasn't consistent. In my DAO, I tried adding an explicit call to getJpaTemplate().getEntityManagerFactory().close(). Of course that didn't work because the method where I put the call was within the transaction, leading to:
org.springframework.dao.InvalidDataAccessApiUsageException: 
Attempting to execute an operation on a closed EntityManager.;
nested exception is java.lang.IllegalStateException:
Attempting to execute an operation on a closed EntityManager.
Therefore, I had to call the method from within the class using the DAO that made the call to the method involved in the transaction.

I wonder if this has something to do with my transaction setup:

<bean id="entityManagerFactory" 
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
<property name="database" value="MYSQL"/>
<property name="showSql" value="true" />
<!-- set to true if you want the vendor to create the database for you on startup -->
<property name="generateDdl" value="false" />
</bean>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>

<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<aop:config>
<!-- interface to my DAO
<aop:advisor pointcut="execution(* *..MyDaoService.*(..))" advice-ref="txAdvice"/>
</aop:config>
If you have any insight, please leave a comment :).

Monday, February 2, 2009

Mounting a Windows share on Linux

I created a share on Windows desktop at work and wanted to access it from my Ubuntu 8.10 virtual machine served up by VMWare Server but ran into a roadblock:
$ sudo mount -t smbfs -o username=myWindowsLogin,password=myWindowsPassword,uid=prystasj,gid=prystasj,file_mode=000,dir_mode=000 //myWindowsBox/share /share
mount: wrong fs type, bad option, bad superblock on //prystash-j.mycompany.org/share,
missing codepage or helper program, or other error
(for several filesystems (e.g. nfs, cifs) you might
need a /sbin/mount. helper program)
In some cases useful info is found in syslog - try
dmesg | tail or so

$ dmesg | tail
...
[19838.333431] smb_fill_super: missing data argument
...
The answer was to install smbfs.
$ sudo apt-get install smbfs
$ sudo mount -t smbfs -o username=myWindowsLogin,password=myWindowsPassword,uid=prystasj,gid=prystasj,file_mode=000,dir_mode=000 //myWindowsBox/share /share
Unfortunately, as myself or root, I'm not able to write anything to /share from my Linux host, but I can read just fine.
$ sudo ls -ld /share
d--------- 1 prystasj prystasj 0 2009-02-02 15:41 /share
Will have to investigate more later.

Sunday, February 1, 2009

Remote Debugging Mule 2.x on Linux

I was having trouble getting my Mule 2.1.1 instance start in debugging mode, until I found this blog entry: Mule Error: Remote Debugging a Mule Server6. The start of my resulting wrapper.conf, with the normal debug line having been broken out into 4:
# Location of your Mule installation.
wrapper.java.additional.1=-Dmule.home="%MULE_HOME%"
wrapper.java.additional.1.stripquotes=TRUE
wrapper.java.additional.2=-Dmule.base="%MULE_BASE%"
wrapper.java.additional.2.stripquotes=TRUE

# Javaagent for certain load time weavers
wrapper.java.additional.3=-javaagent:/opt/mule-2.1.1/lib/user/spring-agent-2.5.3.jar

# Debug remotely, the application will wait for the external debugger to connect.
# original line:
# wrapper.java.additional.4=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
wrapper.java.additional.4=-Xdebug
wrapper.java.additional.5=-Xnoagent
wrapper.java.additional.6=-Djava.compiler=NONE
wrapper.java.additional.7=-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
Then I finally got what I was looking for in my mule.log and was able to attach to port 5005 in Eclipse:
--> Wrapper Started as Daemon
------------------------------------------------------------------------
The JVM is being launched with a debugger enabled and could possibly be
suspended. To avoid unwanted shutdowns, timeouts will be disabled,
removing the ability to detect and restart frozen JVMs.
------------------------------------------------------------------------
Launching a JVM...
Listening for transport dt_socket at address: 5005
Starting the Mule Server...
Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.

I've tried this on both Ubuntu 8.10 and Linux Mint 6 with Mule 2.1.1.

Monday, January 26, 2009

MySQL Dump and Recreate

Here's a little post so I don't forget to dump and recreate a MySQL database.

To dump a database:
$ mysqldump -uroot -ppassword mydb > mydb.mysql
To recreate the database:
$ mysqladmin -uroot -ppassword drop mydb
$ mysqladmin -uroot -ppassword create mydb
$ mysql -uroot -ppassword mydb < mydb.mysql

Friday, January 23, 2009

/usr/bin/ld: cannot find -lperl

I was trying to install pp, the Perl PAR Packager:
$ sudo cpan pp
I got this error:
/usr/bin/ld: cannot find -lperl
collect2: ld returned 1 exit status
make[1]: *** [par] Error 1
make[1]: Leaving directory `/home/prystasj/.cpan/build/PAR-Packer-0.982-zX8GIO/myldr'
make: *** [subdirs] Error 2
SMUELLER/PAR-Packer-0.982.tar.gz
/usr/bin/make -- NOT OK
Running make test
Can't test without successful make
Running make install
Make had returned bad status, install seems impossible
I saw a suggestion to install libperl-dev, but I had that package already up-to-date.
By changing this symlink in /usr/lib from:
lrwxrwxrwx 1 root root 15 2009-01-23 15:28 /usr/lib/libperl.so -> libperl.so.5.10
To:
lrwxrwxrwx 1 root root 15 2009-01-23 15:28 /usr/lib/libperl.so -> libperl.so.5.10.0
I was able to install pp:
$ which pp
/usr/local/bin/pp

Wednesday, January 7, 2009

JPA: Retrieving ID of Recently Persisted Object

Using JPA with an orm.xml, I was having trouble retrieving the primary key (id) of a recently persisted object. The entity in my orm.xml:

<entity class="Foo" name="Foo" access="FIELD">
<table name="Foo"/>
<attributes>
<id name="id">
<column name="ID" nullable="false"/>
<generated-value strategy="SEQUENCE"/>
</id>
...
</table>
</entity>
The relevant MySQL for the table:
CREATE TABLE FOO
(
ID INT NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY(ID)
)
Regardless of whether or not the id property was null or not, it was not updated after the following call to persist():
Foo foo = new Foo();
getJpaTemplate().persist(foo);
if (logger.isDebugEnabled())
logger.debug(foo.getId()) // null

Adding a call to flush() however ended up giving me the id:
Foo foo = new Foo();
getJpaTemplate().persist(foo);
getJpaTemplate().flush();
if (logger.isDebugEnabled())
logger.debug(foo.getId()) // outputs the assigned id
I'm not sure why the flush() was necessary. A similar issue is also discussed here: Spring Forums Post.