Guest User

Untitled

a guest
Dec 7th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
  2. builder.bind("jdbc/Oracle", ods);
  3. builder.activate();
  4.  
  5. DataSource ds = InitialContext.doLookup("jdbc/Oracle");
  6.  
  7. Add the following Jar's to the JUnit test-case's CLASSPATH:
  8.  
  9. TOMCAT_HOME/bin/tomcat-juli.jar (required by catalina.jar)
  10. TOMCAT_HOME/lib/catalina.jar (contains the actual factory)
  11.  
  12. Create the binding that you require in the static "for-all-tests" method:
  13.  
  14. @BeforeClass
  15. public static void setUpClass() throws Exception {
  16. ...
  17. // Use Apache Tomcat's Directory
  18. System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
  19. System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
  20. // Standard hook
  21. InitialContext initialContext = new InitialContext();
  22. // Create binding
  23. initialContext.createSubcontext("java:");
  24. initialContext.createSubcontext("java:comp");
  25. initialContext.createSubcontext("java:comp/env");
  26. initialContext.createSubcontext("java:comp/env/jdbc");
  27. // Construct DataSource
  28. OracleConnectionPoolDataSource dataSource = new OracleConnectionPoolDataSource();
  29. dataSource.setURL("jdbc:oracle:thin:@myserver:1521:MYSID");
  30. dataSource.setUser("username");
  31. dataSource.setPassword("password");
  32.  
  33. initialContext.bind("java:comp/env/jdbc/mydatabase", dataSource);
  34. ...
  35. }
  36.  
  37.  
  38. Then you can create this method in your Singleton class (either lookup method works):
  39.  
  40. public Connection getConnection() throws NamingException, SQLException {
  41. if (dataSource == null) {
  42. Context initialContext = new InitialContext();
  43. boolean bLooksLikeChangeDirectory = false;
  44. if (bLooksLikeChangeDirectory) {
  45. Context context = (Context) initialContext.lookup("java:comp/env");
  46. dataSource = (DataSource) context.lookup("jdbc/mydatabase");
  47. } else {
  48. dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/mydatabase");
  49. }
  50. }
  51. Connection result = dataSource.getConnection();
  52. return result;
  53. }
  54.  
  55. You can use the "com.sun.jndi.fscontext.RefFSContextFactory" (fscontext.jar and providerutil.jar).
  56.  
  57. I wanted "lookup" to be runnable from inside an application server as well, so it would be useful if someone could tell me
  58. for sure if one *has* to use lookup("java:comp/env/jdbc/mydbnickname") instead of lookup("jdbc/mydbnickname") when running
  59. inside an application server.
  60.  
  61. The latter is preferable, because "java:comp/env" does not exist in the stand-alone RefFSContextFactory directory,
  62. so you'd have to have a System property that specifies the JNDI lookup parameter.
  63.  
  64. Overview
  65. You can use "jdbc/mydbnickname" as the argument to "lookup" and to "rebind" (i.e. no "scheme:").
  66. In this case, "RefFSContextFactory" uses the "default scheme", whatever that is ("file:" or "jndi:").
  67. With the following in "jndi.properties" (on the CLASSPATH)
  68. java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory
  69. java.naming.provider.url=file:///u:/workdirectory
  70. and this in "persistence.xml" (JTA setup)
  71. <jta-data-source>jdbc/mydbnickname</jta-data-source>
  72. When you use:
  73. ...rebind("jdbc/mydbnickname", ...)
  74. and
  75. ...lookup("jdbc/mydbnickname")
  76. then the ".bindings" file (created by "rebind") is
  77. "u:/workdirectory/.bindings"
  78. and it looks like this (I have sorted it and tested it with just "lookup"):
  79. #This file is used by the JNDI FSContext.
  80. #Thu Jan 09 16:02:17 EST 2014
  81. jdbc/mydbnickname/ClassName=com.jolbox.bonecp.BoneCPDataSource
  82. jdbc/mydbnickname/FactoryName=com.jolbox.bonecp.BoneCPDataSource
  83. jdbc/mydbnickname/RefAddr/0/Content=oracle.jdbc.OracleDriver
  84. jdbc/mydbnickname/RefAddr/0/Encoding=String
  85. jdbc/mydbnickname/RefAddr/0/Type=driverClassName
  86. jdbc/mydbnickname/RefAddr/1/Content=jdbc:oracle:thin:@myserver:1521:mysid
  87. jdbc/mydbnickname/RefAddr/1/Encoding=String
  88. jdbc/mydbnickname/RefAddr/1/Type=jdbcUrl
  89. jdbc/mydbnickname/RefAddr/2/Content=myusername
  90. jdbc/mydbnickname/RefAddr/2/Encoding=String
  91. jdbc/mydbnickname/RefAddr/2/Type=username
  92. jdbc/mydbnickname/RefAddr/3/Content=mypassword
  93. jdbc/mydbnickname/RefAddr/3/Encoding=String
  94. jdbc/mydbnickname/RefAddr/3/Type=password
  95. If you use
  96. "jndi:jdbc/mydbnickname"
  97. instead of
  98. "jdbc/mydbnickname",
  99. then the file created is
  100. u:/workdirectory/jdbc/.bindings
  101. and it looks like this:
  102. mydbnickname/ClassName=com.jolbox.bonecp.BoneCPDataSource
  103. mydbnickname/FactoryName=com.jolbox.bonecp.BoneCPDataSource
  104. mydbnickname/RefAddr/0/Content=oracle.jdbc.OracleDriver
  105. mydbnickname/RefAddr/0/Encoding=String
  106. mydbnickname/RefAddr/0/Type=driverClassName
  107. mydbnickname/RefAddr/1/Content=jdbc:oracle:thin:@myserver:1521:mysid
  108. mydbnickname/RefAddr/1/Encoding=String
  109. mydbnickname/RefAddr/1/Type=jdbcUrl
  110. mydbnickname/RefAddr/2/Content=myusername
  111. mydbnickname/RefAddr/2/Encoding=String
  112. mydbnickname/RefAddr/2/Type=username
  113. mydbnickname/RefAddr/3/Content=mypassword
  114. mydbnickname/RefAddr/3/Encoding=String
  115. mydbnickname/RefAddr/3/Type=password
  116. Rebind (in a JUnit Test)
  117. @BeforeClass
  118. public static void setUpClass() throws Throwable {
  119. final String sMyName = "setUpClass";
  120. try {
  121. if (Boolean.parseBoolean(System.getProperty("test.initialcontext.rebind", "true"))) {
  122. final InitialContext initialContext = new InitialContext();
  123. final String contextName = "jdbc/mydbnickname";
  124. final Reference contextValue = new Reference("com.jolbox.bonecp.BoneCPDataSource", "com.jolbox.bonecp.BoneCPDataSource", null);
  125. contextValue.add(new StringRefAddr("driverClassName", "oracle.jdbc.OracleDriver"));
  126. contextValue.add(new StringRefAddr("jdbcUrl", "jdbc:oracle:thin:@myserver:1521:mysid"));
  127. contextValue.add(new StringRefAddr("username", "myusername"));
  128. contextValue.add(new StringRefAddr("password", "mypassword"));
  129. initialContext.rebind(contextName, contextValue);
  130. }
  131. } catch (final Throwable exception) {
  132. Utils.getInstance().logExceptionStack(logger, Level.ERROR, sMyName, exception);
  133. throw exception;
  134. }
  135. }
  136. Lookup (in production code)
  137. protected Connection getConnection() throws Exception {
  138. Connection result = null;
  139. // "An InitialContext instance is not synchronized against concurrent access by multiple threads"
  140. synchronized (this) {
  141. if (context == null) {
  142. context = new InitialContext();
  143. }
  144. final BoneCPDataSource connectionPool = (BoneCPDataSource) context.lookup("jdbc/mydbnickname");
  145. result = connectionPool.getConnection();
  146. }
  147. return result;
  148. }
  149. CLASSPATH
  150. BoneCP Connection Pool
  151. <classpathentry kind="var" path="JAVA_LIB/bonecp-0.8.0.RELEASE.jar" sourcepath="/JAVA_LIB/bonecp-0.8.0.RELEASE-sources.jar"/>
  152. <classpathentry kind="var" path="JAVA_LIB/slf4j-api-1.7.5.jar" sourcepath="/JAVA_LIB/slf4j-api-1.7.5-sources.jar"/>
  153. <classpathentry kind="var" path="JAVA_LIB/guava-15.0.jar" sourcepath="/JAVA_LIB/guava-15.0-sources.jar"/>
  154. <classpathentry kind="var" path="JAVA_LIB/slf4j-simple-1.7.5.jar" sourcepath="/JAVA_LIB/slf4j-simple-1.7.5-sources.jar"/>
  155. Eclipse JPA (2.5.1)
  156. <classpathentry kind="var" path="JAVA_LIB/javax.persistence-2.1.0.jar"/>
  157. <classpathentry kind="var" path="JAVA_LIB/eclipselink-2.5.1.jar"/>
  158. JNDI
  159. <classpathentry kind="var" path="JAVA_LIB/fscontext.jar"/>
  160. <classpathentry kind="var" path="JAVA_LIB/providerutil.jar"/>
Add Comment
Please, Sign In to add comment