Partage
  • Partager sur Facebook
  • Partager sur Twitter

[JavaEE] JDBC : no suitable driver found aléatoire

PoolConnection

    15 mai 2022 à 16:20:52

    Bonjours je vous contacte car dans le cadre d'un pool de connexion l'erreur : no suitable driver found apparais aléatoirement cela fait des jours que je cherche sans comprendre : (le jar est inscris grâce a gradle )  pourriez vous me mettre sur la piste merci d'avance 

    PS le beug survient lorsqu'un certain nombre de connexion arrive / ou lors d'un refresh  

    :code 

    package com.T8.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    import com.Model.modelConnection;
    import jakarta.validation.constraints.NotNull;
    
    public class poolConnection {
    
    	private final static int MAX_TIMEOUT = 120;
    	private static  int MAX_POOL_SIZE = 20;
    	private static ConcurrentHashMap<String, List<modelConnection>> connectionPool;
    	private static ConcurrentHashMap<String, String[]> argsById = new ConcurrentHashMap<String, String[]>();
    	private static  int INITIAL_POOL_SIZE = 10;
    	private static int INCREMENT_POOL_SIZE = 5;
    	private static ConcurrentHashMap<String, backgroundConnection> backgroundConnectionById = new ConcurrentHashMap<String, backgroundConnection>();
    	private static ConcurrentHashMap<String, ScheduledExecutorService> refreshConnectionById = new ConcurrentHashMap<String, ScheduledExecutorService>();
    
    	public poolConnection() {
    		// TODO Auto-generated constructor stub
    	}
    
    	public static void create(@NotNull String id, @NotNull String url, @NotNull String user, @NotNull String password)
    			throws SQLException, ClassNotFoundException {
    
    		if (connectionPool == null) {
    			connectionPool = new ConcurrentHashMap<String, List<modelConnection>>();
    		}
    		if (connectionPool.get(id) == null) {
    			System.out.println("connectionPool id : " + id);
    			connectionPool.remove(id);
    			connectionPool.put(id, new ArrayList<modelConnection>(getMinPoolSize()));
    
    		}
    		if (connectionPool.get(id).isEmpty()) {
    			System.out.println("connectionPool pulling id : " + id);
    			List<modelConnection> pool = new ArrayList<>(getMinPoolSize());
    			for (int i = 0; i < getMinPoolSize(); i++) {
    				modelConnection connection = new modelConnection();
    				connection.setConnection(createConnection(url, user, password));
    				connection.setUsed(false);
    				connection.setPosition(i);
    				pool.add(connection);
    			}
    			connectionPool.get(id).addAll(pool);
    		}
    		String[] args = new String[3];
    		args[0] = url;
    		args[1] = user;
    		args[2] = password;
    		argsById.remove(id);
    		argsById.put(id, args);
    
    		if (!backgroundConnectionById.containsKey(id)) {
    			backgroundConnection background = new backgroundConnection(id);
    			backgroundConnectionById.put(id, background);
    		}
    		if (!refreshConnectionById.containsKey(id)) {
    
    			refreshConnection refreshTime = new refreshConnection(id);
    			// running timer task as daemon thread
    			ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
    			service.scheduleAtFixedRate(refreshTime, 30, 60, TimeUnit.SECONDS);
    			refreshConnectionById.put(id, service);
    
    		}
    
    	}
    	// standard constructors
    
    	public static modelConnection getConnection(@NotNull String id) throws SQLException, ClassNotFoundException {
    
    		modelConnection connection = null;
    		if (!connectionPool.get(id).isEmpty()) {
    
    			String[] args = argsById.get(id);
    
    			for (int i = 0; i < connectionPool.get(id).size(); i++) {
    				modelConnection modelConnection = connectionPool.get(id).get(i);
    				// If connection on available list is closed (e.g.,
    				// it timed out), then remove it from available list
    				// and repeat the process of obtaining a connection.
    				// Also wake up threads that were waiting for a
    				// connection because maxConnection limit was reached.
    
    				if (modelConnection.getConnection().isClosed() || !modelConnection.getConnection().isValid(0)) {
    					connectionPool.get(id).get(i).setConnection(createConnection(args[0], args[1], args[2]));
    					connectionPool.get(id).get(i).setUsed(true);
    
    					connection = connectionPool.get(id).get(i);
    
    					if (connection.getConnection().isClosed() || !connection.getConnection().isValid(0)) {
    						connection = createdSelfConnection(id);
    					}
    
    					break;
    				} else if (!modelConnection.isUsed()) {
    					connectionPool.get(id).get(i).setUsed(true);
    					connection = modelConnection;
    					break;
    				}
    				// si la connection est la derniere et qu'elles sont toutes utiliser
    				else if (modelConnection.isUsed() && i == connectionPool.get(id).size()) {
    					if (connectionPool.get(id).size() + getIncrementPoolSize() < getMaxPoolSize()) {
    						System.out.println("connectionPool pulling id : " + id);
    						List<modelConnection> pool = new ArrayList<>(getIncrementPoolSize());
    						for (int j = 0; j < 5; j++) {
    							connection = new modelConnection();
    							connection.setConnection(createConnection(args[0], args[1], args[2]));
    							connection.setUsed(false);
    							connection.setPosition(connectionPool.get(id).size() + j);
    							pool.add(connection);
    						}
    						connectionPool.get(id).addAll(pool);
    					} else {
    						connection = createdSelfConnection(id);
    
    					}
    				}
    			}
    
    		} else {
    			connection = createdSelfConnection(id);
    		}
    
    		return connection;
    
    	}
    
    	private static modelConnection createdSelfConnection(String id) {
    
    		// Three possible cases:
    		// 1) You haven't reached maxConnections limit. So
    		// establish one in the background if there isn't
    		// already one pending, then wait for
    		// the next available connection (whether or not
    		// it was the newly established one).
    		// 2) You reached maxConnections limit and waitIfBusy
    		// flag is false. Throw SQLException in such a case.
    		// 3) You reached maxConnections limit and waitIfBusy
    		// flag is true. Then do the same thing as in second
    		// part of step 1: wait for next available connection.
    
    		modelConnection connection;
    		if ((getSize(id) < getMinPoolSize() + 1)) {
    			makeBackgroundConnection(id);
    			connection = connectionPool.get(id).get(getMinPoolSize() + 1);
    
    		} else {
    			try {
    				synchronized (backgroundConnectionById.get(id)) {
    					backgroundConnectionById.get(id).wait(); // wait
    				}
    			} catch (InterruptedException ie) {
    			}
    			// Someone freed up a connection, so try again.
    			connection = createdSelfConnection(id);
    		}
    		// Wait for either a new connection to be established
    		// (if you called makeBackgroundConnection) or for
    		// an existing connection to be freed up.
    
    		return connection;
    	}
    
    	private static void makeBackgroundConnection(String id) {
    
    		if (connectionPool.get(id).size() >= getMinPoolSize() + 1) {
    			connectionPool.get(id).remove(getMinPoolSize() + 1);
    		}
    		try {
    			Thread connectThread = new Thread(backgroundConnectionById.get(id));
    			connectThread.start();
    		} catch (OutOfMemoryError oome) {
    			// Give up on new connection
    		}
    	}
    
    	public static void releaseConnection(@NotNull String id, @NotNull modelConnection connection) {
    		connectionPool.get(id).get(connection.getPosition()).setUsed(false);
    		if (connection.getPosition() > getMinPoolSize()) {
    			connectionPool.get(id).remove(connection.getPosition());
    		}
    	}
    
    	private static Connection createConnection(String url, String user, String password)
    			throws SQLException, ClassNotFoundException {
    		Class.forName("com.mysql.cj.jdbc.Driver");
    System.out.println("jdbc:mysql://184.154.119.210:3306/" + url);
    		return DriverManager.getConnection("jdbc:mysql://184.154.119.210:3306/" + url, user, password);
    	}
    
    	public static int getSize(String id) {
    
    		System.out.println("connectionPool id : " + id + " size : " + connectionPool.get(id).size());
    
    		return connectionPool.get(id).size();
    	}
    
    	public static int getSizeUsed(String id) {
    		int used = 0;
    
    		for (modelConnection connection : connectionPool.get(id)) {
    			if (connection.isUsed()) {
    				used++;
    			}
    		}
    
    		return used;
    	}
    
    	public static int getIncrementPoolSize() {
    		return INCREMENT_POOL_SIZE;
    	}
    
    	public void setIncrementPoolSize(int incrementPoolSize) {
    		INCREMENT_POOL_SIZE = incrementPoolSize;
    	}
    
    	public static int getMinPoolSize() {
    		return INITIAL_POOL_SIZE;
    	}
    
    	public static void setMinPoolSize(int initialPoolSize) {
    		INITIAL_POOL_SIZE = initialPoolSize;
    	}
    
    	public static int getMaxPoolSize() {
    		return MAX_POOL_SIZE;
    	}
    
    	public static void setMaxPoolSize(int maxPoolSize) {
    		MAX_POOL_SIZE = maxPoolSize;
    	}
    
    	public static class backgroundConnection implements Runnable {
    
    		String id;
    
    		public backgroundConnection(String id) {
    			this.id = id;
    		}
    
    		@Override
    		public void run() {
    			try {
    				String[] args = argsById.get(id);
    
    				synchronized (this) {
    					modelConnection connection = new modelConnection();
    					connection.setConnection(poolConnection.createConnection(args[0], args[1], args[2]));
    					connection.setUsed(true);
    					connection.setPosition(getSize(id) + 1);
    					connectionPool.get(id).add(connection);
    					notifyAll();
    				}
    			} catch (Exception e) { // SQLException or OutOfMemory
    				// Give up on new connection and wait for existing one
    				// to free up.
    				e.printStackTrace();
    			}
    
    		}
    	}
    
    	public static class refreshConnection implements Runnable {
    		String id;
    
    		public refreshConnection(String id) {
    			this.id = id;
    		}
    
    		@Override
    		public void run() {
    			String[] args = argsById.get(id);
    			for (int i = 0; i < connectionPool.get(id).size(); i++) {
    				try {
    					if (!connectionPool.get(id).get(i).getConnection().isValid(0)
    							&& !connectionPool.get(id).get(i).isUsed()
    							|| connectionPool.get(id).get(i).getConnection().isClosed()
    									&& !connectionPool.get(id).get(i).isUsed()) {
    						connectionPool.get(id).get(i).getConnection().close();
    						connectionPool.get(id).get(i)
    								.setConnection(poolConnection.createConnection(args[0], args[1], args[2]));
    					}
    				} catch (Exception e) {
    					System.out.println("error :" + e.getMessage());
    
    				}
    			}
    
    		}
    
    	}
    
    }
    



    -
    Edité par Project T48 16 mai 2022 à 10:26:03

    • Partager sur Facebook
    • Partager sur Twitter

    Honor Is Freedom

    [JavaEE] JDBC : no suitable driver found aléatoire

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    • Editeur
    • Markdown