Unfortunately I'm closing my blog this week. I can't spend time with posting and moderating out Chinese people who write junk comments all the time.
I have had ~9000 visitor from 111 country all over the world in the last two year, thanks for it!
Bye,
...szpetip
Sunday, July 4, 2010
Saturday, July 4, 2009
String puzzle
A short, but interesting puzzle.
Question
What does the following program print:
Answer
When you run the code, you get the answer: 2.
Why?
The compiler translates Unicode escapes into the characters they represent before it parses the program into tokens, such as strings literals. As you see, NetBeans IDE doesn't know it, it present the code like a string with a length() method.
Question
What does the following program print:
Answer
When you run the code, you get the answer: 2.
Why?
The compiler translates Unicode escapes into the characters they represent before it parses the program into tokens, such as strings literals. As you see, NetBeans IDE doesn't know it, it present the code like a string with a length() method.
Saturday, May 16, 2009
NetBeans 6.5.1 generates invalid web.xml
If you use NetBeans 6.5.1 and you want to add a new servlet to the project, the IDE generate an invalid web.xml file and you will be not able to deploy the application.
The problem
If you try to deploy the web application into an application container, then you get an exception like this:
The solution
Solving this problem is very easy:
The problem
If you try to deploy the web application into an application container, then you get an exception like this:
org.apache.catalina.startup.ContextConfig applicationWebConfig
SEVERE: Parse error in application web.xml file at jndi:/localhost/XXXXXX/WEB-INF/web.xml
java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name XXXXXX
The solution
Solving this problem is very easy:
- Open your web.xml file and choose the XML view to see the xml code.
- Cut all <servlet-mapping> tags and put them back after the <servlet> tags.
Friday, May 15, 2009
Solve MySQL server drops and close connections in connection pool
If you get "Last packet sent to the server was XX ms ago." error in your application that want to access MySQL database or you want to know, how to create a connection pool using Tomcat server and DataSource then this is your post.
Connection pooling with MySQL
Using connection pool is a good way to increase your web application speed. If you create a connection pool in your application, using a MySQl server, in the first morning you will see, the database server drops all of the connections in the pool.
This is because MySQL drops connections after 8 hours idle period. There are many solution in the web, some are working, some aren't working, but the most of them are hacking.
I present an elegant solution to this typically problem, caused by MySQL server.
The best solution - Commons DBCP
Download Commons DBCP
The Apache Commons project has a very useful connection pool implementation, called Commons DBCP.
To use Commons DBCP,
Configuring datasource - Tomcat server, server.xml
In my solution, the datasource is defined as a global resource in the server.xml of the Tomcat. To do this, edit the server.xml file in the conf directory in your Tomcat server and add some code line like this between the <GlobalNamingResources> </GlobalNamingResources> tags:
Parameters descriptions are availables here, in nutshell:
name - The datasource will be registred with this name in the naming context.
auth - The authentication type of the resource. Set to Container.
type - The resource type. Set to javax.sql.DataSource to create a datasource.
maxAcive - The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.
maxIdele - The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.
maxWait - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely.
timeBetweenEvictionRunsMillis - The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.
minEvictableIdleTimeMillis - The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).
factory - The factory, that provide the connections, to use Commons DBCP, use org.apache.commons.dbcp.BasicDataSourceFactory.
userName - The user name to access your database.
password - The password to access your database.
driverClassName - The driver class to access the database, in this case com.mysql.jdbc.Driver. The driver file must be copied into the lib directory of the Tomcat server.
url - The database url.
testOnBorrow - The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another. Set to true.
testOnReturn - The indication of whether objects will be validated before being returned to the pool. Set to true.
validationQuery - The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query must be an SQL SELECT statement that returns at least one row.
Configuring datasource - Web application, context.xml
To access the defined global resource, modify the context.xml file in your web application. Open the context.xml with your favorite editor and put the following code between the <Context> </Context> tags:
The global parameter is the global name of the defined resource in the server.xml, the name parameter is the name of the resource in our context, type is like in server.xml, the type of the resource, in this case javax.sql.DataSource.
Getting a connection from the pool
We're ready to get a connection from the pool and use it in a Hibernate transaction. The code:
Tips
You must restart your Tomcat server after you copy something into lib directory or edit the server.xml file.
Connection pooling with MySQL
Using connection pool is a good way to increase your web application speed. If you create a connection pool in your application, using a MySQl server, in the first morning you will see, the database server drops all of the connections in the pool.
This is because MySQL drops connections after 8 hours idle period. There are many solution in the web, some are working, some aren't working, but the most of them are hacking.
I present an elegant solution to this typically problem, caused by MySQL server.
The best solution - Commons DBCP
Download Commons DBCP
The Apache Commons project has a very useful connection pool implementation, called Commons DBCP.
To use Commons DBCP,
- download the required jar file from the project page (DBCP Download),
- copy it into the lib directory in your Tomcat server and add to your project, where you want to use the connection pool.
Configuring datasource - Tomcat server, server.xml
In my solution, the datasource is defined as a global resource in the server.xml of the Tomcat. To do this, edit the server.xml file in the conf directory in your Tomcat server and add some code line like this between the <GlobalNamingResources> </GlobalNamingResources> tags:
<resource name="jdbc/sampleDB" auth="Container" type="javax.sql.DataSource" maxActive="15" maxidle="10" maxwait="10000" timeBetweenEvictionRunsMillis="10000" minEvictableIdleTimeMillis="60000" factory="org.apache.commons.dbcp.BasicDataSourceFactory" username="XXXXXXXX" password="XXXXXXX" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/sampledatabase" testOnBorrow="true" testOnReturn="true" validationQuery="select count(*) from TABLENAME" />
Parameters descriptions are availables here, in nutshell:
name - The datasource will be registred with this name in the naming context.
auth - The authentication type of the resource. Set to Container.
type - The resource type. Set to javax.sql.DataSource to create a datasource.
maxAcive - The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.
maxIdele - The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.
maxWait - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely.
timeBetweenEvictionRunsMillis - The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.
minEvictableIdleTimeMillis - The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).
factory - The factory, that provide the connections, to use Commons DBCP, use org.apache.commons.dbcp.BasicDataSourceFactory.
userName - The user name to access your database.
password - The password to access your database.
driverClassName - The driver class to access the database, in this case com.mysql.jdbc.Driver. The driver file must be copied into the lib directory of the Tomcat server.
url - The database url.
testOnBorrow - The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another. Set to true.
testOnReturn - The indication of whether objects will be validated before being returned to the pool. Set to true.
validationQuery - The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query must be an SQL SELECT statement that returns at least one row.
Configuring datasource - Web application, context.xml
To access the defined global resource, modify the context.xml file in your web application. Open the context.xml with your favorite editor and put the following code between the <Context> </Context> tags:
<ResourceLink global="jdbc/sampleDB" name="jdbc/sampleDB" type="javax.sql.DataSource"/>
The global parameter is the global name of the defined resource in the server.xml, the name parameter is the name of the resource in our context, type is like in server.xml, the type of the resource, in this case javax.sql.DataSource.
Getting a connection from the pool
We're ready to get a connection from the pool and use it in a Hibernate transaction. The code:
...
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
...
public void someMethod() throws Exception {
//Getting the initial context.
Context initCtx = new InitialContext();
//Getting the env context from the initial context,
// where the resources are defined.
Context envCtx = (Context) initCtx.lookup("java:comp/env");
//Getting the datasource from the env context.
//The lookup parameter must be the name parameter
// of the ResourceLink tag in the application's context,xml.
DataSource dataSource = (DataSource) envCtx.lookup("jdbc/sampleDB");
//Getting a connection from the datasource.
Connection connection = dataSource.getConnection();
//sessionFactory must be set before use here, setting
// sessiobFactory is depend on the type of Hibernate
// type and version.
try {
Session session = sessionFactory.openSession(connection);
...
//Use the session
...
} catch(Exception ex) {
//Very important exception handling methods comes here
...
} finally {
//finally statement is very important if you use connection
// from datasource. if you don't use then the connection
// can be stuck and it will be not more available.
try {
//If you use datasource, closing the sessions are as important as
// opeining them.
session.close();
} catch(Excedption ex) { ... }
}
...
Tips
You must restart your Tomcat server after you copy something into lib directory or edit the server.xml file.
Labels:
commons dbcp,
connection pool,
context.xml,
database,
datasource,
hibernate,
java,
mysql,
server.xml,
tomcat,
web application
Tuesday, February 3, 2009
Did you know: Static import
Static Import is a very simply and unnecessary opportunity in Java.
Static Import
Static Import
The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:
import static java.lang.Math.PI;or:
import static java.lang.Math.*;
Once the static members have been imported, they may be used without qualification:
double r = cos(PI * theta);
It's easy and unnecessary as I said.
Monday, February 2, 2009
Protected Access Modifier Facts
I show an interesting example to the protected access modifier.
Our first file, Animal.java in package world:
Our second file, Animal.java in package africa:
Our third file, Lion.java in package africa:
Our first file, Animal.java in package world:
01. package world;
02. public class Animal {
03. protected int age;
04. }
Our second file, Animal.java in package africa:
01. package africa;
02. import world.Animal;
03. public class Zebra extends Animal {
04. public void sayAge() {
05. System.out.println("My age is "+age); //Works well, age is protected;
06. }
07. public void sayFriendAge() {
08. Animal friend = new Animal();
09. System.out.println("My fried's age is " + friend.age ); //Compile error, age is accessible
10. // through inheritance
11. }
12. }
Our third file, Lion.java in package africa:
01. package africa;
02. public class Lion {
03. public void sayZebraAge() {
04. Zebra zebra = new Zebra();
05. System.out.println("Zebra's age is " + zebra.age ) ; //Compile error, Zebra and Lion are
06. // in the same package, but age is protected and only available using inheritance, Lion
07. // can't see it.
08. }
09. }
Wednesday, December 31, 2008
Some interesting facts from year 2008
You, visitors
I have had visitors from 73 country. This countries from Google Analytics:
On the map:

Search Engines
I searched the "Java Blog" (include quote marks) expression in some search engine, the result was:
Keyword java give 34,600,000 results in Google, 258,000,000 in MSN, 70,000,000 in Yahoo.
Posts
20 posts have been written, this is the 21th. First post is written at June 21, 2008.
Comments
I have had only one comment from you, I hope number of comments will be increased.
I have had visitors from 73 country. This countries from Google Analytics:
|
|
|

Search Engines
I searched the "Java Blog" (include quote marks) expression in some search engine, the result was:
- Google: I was the 41, Google PageRank give me 1/10.
- MSN: I tried, but didn't find myself in first 100.
- Yahoo: I tried, but didn't find myself in first 100.
Keyword java give 34,600,000 results in Google, 258,000,000 in MSN, 70,000,000 in Yahoo.
Posts
20 posts have been written, this is the 21th. First post is written at June 21, 2008.
Comments
I have had only one comment from you, I hope number of comments will be increased.
Subscribe to:
Posts (Atom)