java:java
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:java [2022/07/26 06:51] – admin | java:java [2024/05/31 05:22] (current) – [Random in Java] admin | ||
---|---|---|---|
Line 401: | Line 401: | ||
// so add 1 to make it inclusive | // so add 1 to make it inclusive | ||
ThreadLocalRandom.current().nextInt(min, | ThreadLocalRandom.current().nextInt(min, | ||
+ | </ | ||
+ | ==== Md5 ==== | ||
+ | <code java> | ||
+ | import java.util.HashMap; | ||
+ | import java.security.MessageDigest; | ||
+ | public static String md5(String original) { | ||
+ | try{ | ||
+ | MessageDigest md = MessageDigest.getInstance(" | ||
+ | md.update(original.getBytes()); | ||
+ | byte[] digest = md.digest(); | ||
+ | StringBuffer sb = new StringBuffer(); | ||
+ | for (byte b : digest) { | ||
+ | sb.append(String.format(" | ||
+ | } | ||
+ | return sb.toString(); | ||
+ | }catch(Exception e){ | ||
+ | return ""; | ||
+ | } | ||
+ | } | ||
</ | </ | ||
==== Java Reflection ==== | ==== Java Reflection ==== | ||
Line 562: | Line 581: | ||
* **Atomic variables** have features that minimize synchronization and help avoid memory consistency errors. | * **Atomic variables** have features that minimize synchronization and help avoid memory consistency errors. | ||
* **ThreadLocalRandom** (in JDK 7) provides **efficient generation of pseudorandom numbers from multiple threads**. | * **ThreadLocalRandom** (in JDK 7) provides **efficient generation of pseudorandom numbers from multiple threads**. | ||
- | === Executor Interfaces === | + | |
- | === Thread pools === | + | ==== Thread pools ==== |
+ | {{: | ||
refer: | refer: | ||
- | * https://docs.oracle.com/javase/ | + | * https://www.baeldung.com/thread-pool-java-and-guava |
- | * http:// | + | |
* http:// | * http:// | ||
+ | * http:// | ||
Thread pools address two different problems: | Thread pools address two different problems: | ||
* They usually provide improved performance when **executing large numbers of asynchronous tasks**, due to reduced per-task invocation overhead | * They usually provide improved performance when **executing large numbers of asynchronous tasks**, due to reduced per-task invocation overhead | ||
Line 1239: | Line 1259: | ||
log4j.appender.hibernateAppender.DatePattern=' | log4j.appender.hibernateAppender.DatePattern=' | ||
log4j.category.org.hibernate=INFO, | log4j.category.org.hibernate=INFO, | ||
+ | </ | ||
+ | ===== Java Connection Pool ==== | ||
+ | Connection pooling is the technique used to increase the performance of the application when an | ||
+ | Connection pooling takes care of **managing connections(prepare, | ||
+ | * Connection Pool API is responsible for opening N connections and leave them ready for our application. | ||
+ | * Our application will just ask for the pool to get a new connection, it will use it and then deliver it back to the Pool. | ||
+ | * If a connection is **stale**, the pooling mechanism would then **close it and re-open a new one**. | ||
+ | * This way we can use the connections in a more better way, as **we no need to wait for the connection to be established during the actual execution of our code** and we don’t have to worry about stale connections | ||
+ | refer: | ||
+ | * https:// | ||
+ | * {{: | ||
+ | * {{: | ||
+ | |||
+ | ==== Default Hibernate connection pooling ==== | ||
+ | Below are basic steps config for connections in Hibernate(each connection in MySQL was a session in Hibernate)(refer: | ||
+ | - Config max connections in MySQL: | ||
+ | * config temporary in MySQL Admin console:< | ||
+ | SET GLOBAL MAX_CONNECTIONS = 200; | ||
+ | </ | ||
+ | * config in **my.cnf or my.ini** of MySQL:< | ||
+ | MAX_CONNECTIONS = 200 | ||
+ | </ | ||
+ | - Config max pool connections:< | ||
+ | < | ||
+ | </ | ||
+ | - coding to **close session** after call(in **doWork()** function):< | ||
+ | public class DBConnection { | ||
+ | private static SessionFactory factory; | ||
+ | static { | ||
+ | factory = new Configuration().configure().buildSessionFactory(); | ||
+ | } | ||
+ | public Session getSession() { | ||
+ | return factory.openSession(); | ||
+ | } | ||
+ | |||
+ | public void doWork() { | ||
+ | Session session = getSession(); | ||
+ | // do work. | ||
+ | session.close(); | ||
+ | } | ||
+ | // Call this during shutdown | ||
+ | public static void close() { | ||
+ | factory.close(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | Most important Hibernate JDBC properties:< | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | ==== org.hibernate.connection.C3P0ConnectionProvider with c3p0 connection pool ==== | ||
+ | refer: | ||
+ | * https:// | ||
+ | * Configuration c3p0: http:// | ||
+ | * org.hibernate.connection Interface ConnectionProvider< | ||
+ | public interface ConnectionProvider | ||
+ | A strategy for obtaining JDBC connections. | ||
+ | |||
+ | Implementors might also implement connection pooling. | ||
+ | |||
+ | The ConnectionProvider interface is not intended to be exposed to the application. Instead it is used internally by Hibernate to obtain connections. | ||
+ | | ||
+ | Release all resources held by this provider. | ||
+ | | ||
+ | Dispose of a used connection. | ||
+ | | ||
+ | Initialize the connection provider from given properties. | ||
+ | | ||
+ | Grab a connection, with the autocommit mode specified by hibernate.connection.autocommit. | ||
+ | | ||
+ | Does this connection provider support aggressive release of JDBC connections and re-acquistion of those connections (if need be) later? | ||
+ | This is used in conjunction with org.hibernate.cfg.Environment.RELEASE_CONNECTIONS to aggressively release JDBC connections. | ||
+ | </ | ||
+ | * org.hibernate.connection.C3P0ConnectionProvider https:// | ||
+ | import javax.sql.DataSource; | ||
+ | import com.mchange.v2.c3p0.DataSources; | ||
+ | ..... | ||
+ | public class C3P0ConnectionProvider | ||
+ | implements ConnectionProvider, | ||
+ | ..... | ||
+ | private DataSource ds; | ||
+ | @Override | ||
+ | public Connection getConnection() throws SQLException { | ||
+ | final Connection c = ds.getConnection(); | ||
+ | ..... | ||
+ | @Override | ||
+ | public void configure(Map< | ||
+ | ..... | ||
+ | final DataSource unpooled = DataSources.unpooledDataSource( jdbcUrl, connectionProps ); | ||
+ | ds = DataSources.pooledDataSource( unpooled, allProps ); | ||
+ | ..... | ||
+ | @Override | ||
+ | public void stop() { | ||
+ | try { | ||
+ | DataSources.destroy( ds ); | ||
+ | } | ||
+ | catch (SQLException sqle) { | ||
+ | C3P0_MSG_LOGGER.unableToDestroyC3p0ConnectionPool( sqle ); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | Download jar for using hibernate c3p0: | ||
+ | * Step1: Download [[http:// | ||
+ | * Step2: Download [[https:// | ||
+ | Hibernate uses its org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider for connection pooling if you **set the hibernate.c3p0.* properties**. properties.< | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | Some basic pool configuration in hibernate-c3p0: | ||
+ | * connnection pool: <code xml> | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | * Global statements pool(statements cached): <code xml> | ||
+ | < | ||
</ | </ | ||
===== Using external libraries for coding java with eclipse ===== | ===== Using external libraries for coding java with eclipse ===== |
java/java.1658818301.txt.gz · Last modified: 2022/10/29 16:15 (external edit)