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 > $

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");
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" />

web.xml :

<description>Oracle Datasource example</description>

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
The above can be configured in context.xml along with maxActive and maxIdle parameters.

Refer to following links for detail explanations:

No comments: