Monday, September 24, 2007

MISC (Shell scripts, Java)

Shell script to remove ^M charactors from a file ftped through pc.

sed 's/^M//;s/^Z//' $1 > $1.new

To type ^M, manually enter with (ctrl+v, ctrl+m). Same for ^Z.

Customize SQLExeption's e.getMessage()

catch(SQLException e)
{
if(e.getErrorCode()==1 && e.getMessage().contains("unique constraint")) {
SQLException se=new SQLException("YOUR CUSTOMIZED MESSAGE");
se.setStackTrace(e.getStackTrace());
throw se;
}
else throw e;
}



Connection Pooling configuration and sample usage using JNDI-DBCP

context.xml :

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/appname" docBase="appname">
<Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource" username="user" password="pass" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@ipaddr:1521:sid" maxActive="8" maxIdle="4" />
</Context>


web.xml :

<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

Code sample :

Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();

Also, some useful parameters you can use to avoid the resource leaks due to connection pooling
removeAbandoned="true"
removeAbandonedTimeout="600"
logAbandoned="true"
The above can be configured in context.xml along with maxActive and maxIdle parameters.

Refer to following links for detail explanations:
http://commons.apache.org/dbcp/
http://tomcat.apache.org/tomcat-4.1-doc/jndi-datasource-examples-howto.html
http://commons.apache.org/dbcp/configuration.html

No comments: