Shipping Company Website Template Free Download

How to create a completely responsive Online Shipping Company Website Template Design HTML CSS and JavaScript. Shipping Company Website Template Free Download

Database Connection Pool

Consider, a live application has several users. The application communicates with the database based on user requests. Since communication with the database is required, I can provide a dedicated connection per user if desired. That is, I will arrange that this connection is never closed. It will survive if the system users are less, no problem. But if the users are too many, then several problems will be created in the database server.

Again, since I’m using a single connection for each request, the requests are waiting for the previous request to complete. So my application performance is also getting worse.

So it appears that providing a dedicated connection is not a good solution. Another way is to open one connection per request and close it again after work. But opening and closing database connections is expensive and slow. This involves CPU and memory allocation. Therefore, connection open/close on each request requires comparatively more time to serve the request of live users. This is where the idea of connection pooling comes in.

Shipping Company Website Template Free Download

What is connection pooling?

In short, a connection pool is a cache of connections that can be used repeatedly by applications. A container of connections, from which I will take connections and use them in the application, and put them back after work.

Since in this case I don’t need to open/close the connection repeatedly, due to which it takes relatively less time to take and return the connection from the pool.

Connection pooling can be of two types: static and dynamic. The number of connections in a static pool is always the same. It cannot be increased even if it is necessary. And the number of connections in the dynamic on-demand pool increases. If not required, the number of connections can also be reduced if desired. I am writing about static connection here.

Now let’s write a basic connection pool code. As soon as the application starts, take n connections and put them in a list.

public void createConnections(int n) {

  try {

      MAX_CONNECTION = n;

      Class.forName(“com.mysql.jdbc.Driver”);

      for(int i = 0;i<n; i++) {

          Connection connection = DriverManager.getConnection(

                  “jdbc:mysql://localhost:3306/testDB”,”root”,”1234″);

          connections.add(connection);

      }

  } catch (Exception ex) {

      ex.printStackTrace();

  }

}

Here connections is an arraylist, defined as:

private ArrayList<Connection> connections = new ArrayList<>();

MAX_CONNECTION is an integer.

Now I am showing the code to take connection from the pool. If there are free connections in the list, the last connection will be returned. Since there may not be free connection, I am returning optional. Caller method will check if free connection is received or not.

public Optional<Connection> getConnectionFromPool() {

  Connection connection = null;

  synchronized (this) {

      if (connections.size() > 0) {

          connection = connections.get(connections.size() – 1);

          connections.remove(connection);

      }

  }

  return Optional.ofNullable(connection);

}

The code to return to the pool after completing the connection is also given. I returned the connection that I was using. I added the connection to the list again.

public boolean giveConnectionBackToConnectionPool(Connection connection) {

  synchronized (this) {

      if (connections.size() < MAX_CONNECTION) {

          connections.add(connection);

          return true;

      }

  }

  return false;

}

The entire code is provided in the git repo. You can try implementing dynamic connection pool.

There are several connection pool frameworks for Java. One of them is:

  Apache Common DBCP

  HikariCP

  C3PO

  BoneCP

  Tomcat JDBC CP

Tomcat JDBC Connection Pool was the default in Spring Boot 1.x. Hikari CP is used by default in Spring Boot 2.x. Hikari CP tie is more popular now. Because it is fast, highly optimized and lightweight compared to others.

Java Stream – takeWhile() and dropWhile()

An important feature of Java 8 is Streams. This provides a test of the declarative paradigm in Java. Several new methods are introduced in Stream in Java 9 to make the work of developers easier. Two of these are: dropWhile() and takeWhile().

dropWhile():

It is an intermediate operation of Stream, which returns another Stream. This method takes one element at a time from the Stream, and compares it with the given condition. If the result of the condition returns true, then the element is dropped. Adds to the stream if it returns false. We can compare it to a normal loop. If the condition of the loop is false, then the loop is exited, similarly, once the result of the condition is false for any element of the stream, all the subsequent elements will come one by one in the stream returned by dropWhile().

String collect = IntStream.range(1, 10)

      .dropWhile(value -> value < 4)

      .mapToObj(i -> ((Integer) i).toString())

      .collect(Collectors.joining(“, “));

System.out.println(collect);

If we run this code, we will see that the output is coming like this.

4, 5, 6, 7, 8, 9

Since 1 to 3 are smaller than 4, dropWhile() drops these three numbers. He took the rest of the numbers into the stream. Let’s see another example:

String s = IntStream.of(1, 10, 9, 20, 5, 4, 3, 2, 12)

      .dropWhile(i -> i < 12)

      .mapToObj(i -> ((Integer) i).toString())

      .collect(Collectors.joining(“, “));

System.out.println(s);

Here, since the first three elements of IntStream are less than 12, dropWhile() will not take them to the next stream. Since 20 is greater than 12, the condition is false. So all remaining elements from 20 will come in the next stream. It doesn’t matter if the next integers are greater than or equal to 12. That means the output will be:

20, 5, 4, 3, 2, 12

takeWhile():

takeWhile() is the exact opposite of dropWhile(). It is also an intermediate operation, which returns another stream. Based on the given condition, it starts fetching the data in the next stream, whenever the result of the condition is false, it stops fetching the element in the stream.

String collect = IntStream.range(1, 10)

      .takeWhile(value -> value < 4)

      .mapToObj(i -> ((Integer) i).toString())

      .collect(Collectors.joining(“, “));

System.out.println(collect);

If you run this code, you will see that the output is coming like this:

1, 2, 3

Since 1, 2 and 3 are smaller than 4, the condition results in true, so takeWhile() takes them into the stream. Whenever 4 occurs, the condition results in false, so the stream stops. In normal loop parlance that’s what we call breaking.

Shipping Company Website Template Free Download

Now if we run this code below:

String collect = IntStream.of(10, 3, 2, 1, 12, 5, 100)

      .takeWhile(i -> i < 7)

      .mapToObj(i -> ((Integer) i).toString())

      .collect(Collectors.joining(“, “));

System.out.println(collect);

I will see that nothing is output. Because the condition has failed for the first integer 10. As a result takeWhile() stopped taking in the stream. Due to which in this case an Empty String comes in the collect string.

Then I saw that dropWhile() keeps dropping elements from the beginning of a stream until the given condition fails. Once the condition fails, it takes all the remaining elements of the stream. And takeWhile() takes elements from the beginning of a stream until the given condition fails. Once the condition is false, it takes no more elements of the stream.

How to create a completely responsive Online Shipping Company Website Template Design HTML CSS and JavaScript

Before Download

You must Join our Facebook Group and Subscribe YouTube Channel

All Links in Below:






Join Our FreeWebsiteCreate Facebook Group to get an instant update for projects, templates, design resources, and solutions.

Join Our YouTube Channel & Subscribe with Bell Icon for New Video:

Join Our Official Facebook Page For the Latest updates All Code Projects are Free:

Visit our service page to get premium services.

Free Website Create – HTML CSS, PHP, JavaScript Programming Projects For Free

Follow Us

Thank You,

Stay with FreeWebsiteCreate.net

Share the post if necessary.

Before Download

You must Join our Facebook Group and Subscribe YouTube Channel




FreeWebsiteCreate.net tries to provide HTML, CSS, SCSS, JavaScript, React, Android Studio, Java, PHP, Laravel, Python, Django, C#(C Sharp), and ASP.net-related projects 100% free. We try to make learning easier. Free Website Create always tries to give free projects to new learners. Free projects and source code will help to learn quickly.

They can save time and learn more. In this post, we share a free portfolio project website code with HTML and CSS. This free code portfolio contains a single landing page with a responsive design. In this post, we get a free best carpenter and craftsman service website designed by FreeWebsiteCreate with HTML, CSS, Bootstrap, and JavaScript.

To get a free website project code,

Keep bookmarking our website, Save categories links, Follow Social Media, Follow the youtube channel Join Facebook groups.

Stay with FreeWebsiteCreate.net

Share the post if necessary.

Leave a Comment