ConnectionManager

东郭勇
2023-12-01
import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;


public class ConnectionManager {

	private final static String JNDI_Name = "java:comp/env/jdbc/sso";

	private static javax.sql.DataSource dataSource = null;

    public static Connection getConnection()
	{
    	if (dataSource==null)
    	{
    		synchronized(ConnectionManager.class)
			{
    			if (dataSource==null)
    			{
			    	Context initCtx = null;
			        try
			        {
			        	String jndiName = System.getProperty("sso.jndiName");
			        	if (jndiName==null || "".equals(jndiName))
							jndiName=JNDI_Name;
			            initCtx = new InitialContext();
			            dataSource = (DataSource) initCtx.lookup(jndiName);
			        }
			        catch (Exception e)
					{
			            e.printStackTrace();
					}
    			}
			}
    	}
    	try
		{
    		if (dataSource!=null)
    			return dataSource.getConnection();
    		else
    		{
    			System.out.println("dataSource is null");
    		}
		}
    	catch (Exception e)
		{
    		e.printStackTrace();
		}
   		return null;
	}

}

 

写道
<Context path="newcqa" reloadable="true" docBase="D:\workplace\company3\xx\WebContent"  workDir="D:\workplace\company3\xx\work" >
<Resource name="jdbc/newcqa"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:uname/upass@IP:1521:ora9i"
username="uname"
password="upass"
maxIdle="30" maxWait="5000" maxActive="100"/>
</Context>

 

 类似资料:

相关阅读

相关文章

相关问答